Michael Rey commited on
Commit
3adc61e
ยท
1 Parent(s): 0baccc0

initial commit

Browse files
Files changed (3) hide show
  1. WA_Fn-UseC_-Telco-Customer-Churn.csv +0 -0
  2. app.py +123 -0
  3. requirements.txt +6 -0
WA_Fn-UseC_-Telco-Customer-Churn.csv ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import seaborn as sns
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.preprocessing import StandardScaler
8
+ from sklearn.svm import SVC
9
+ from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
10
+
11
+ # Custom Streamlit styling with sticky navbar
12
+ st.markdown(
13
+ """
14
+ <style>
15
+ body {
16
+ background-color: #1E1E1E;
17
+ color: #FFFFFF;
18
+ font-family: 'Arial', sans-serif;
19
+ }
20
+ .stButton>button {
21
+ background-color: #4A90E2;
22
+ color: #FFFFFF;
23
+ border-radius: 15px;
24
+ padding: 12px 24px;
25
+ font-size: 16px;
26
+ font-weight: bold;
27
+ }
28
+ .title {
29
+ color: #64FFDA;
30
+ text-shadow: 1px 1px #FF4C4C;
31
+ }
32
+ .stTabs [data-testid="stHorizontalBlock"] {
33
+ position: sticky;
34
+ top: 0;
35
+ background-color: #1E1E1E;
36
+ z-index: 10;
37
+ }
38
+ </style>
39
+ """,
40
+ unsafe_allow_html=True
41
+ )
42
+
43
+ # Load the Telco Customer Churn dataset
44
+ st.title("๐Ÿ“ฒ Telco Customer Churn Prediction")
45
+ st.markdown("<h2 class='title'>Predict whether a customer will churn! ๐Ÿš€</h2>", unsafe_allow_html=True)
46
+
47
+ # Load dataset
48
+ file_path = 'WA_Fn-UseC_-Telco-Customer-Churn.csv'
49
+ df = pd.read_csv(file_path)
50
+
51
+ # Preprocess data and train model (runs once)
52
+ df = df[['tenure', 'MonthlyCharges', 'TotalCharges', 'Churn']]
53
+ df = df.replace(" ", np.nan).dropna()
54
+ df['TotalCharges'] = pd.to_numeric(df['TotalCharges'])
55
+ df['Churn'] = df['Churn'].apply(lambda x: 1 if x == 'Yes' else 0)
56
+
57
+ # Define features and target
58
+ X = df.drop('Churn', axis=1)
59
+ y = df['Churn']
60
+
61
+ # Split data
62
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
63
+
64
+ # Scale data
65
+ scaler = StandardScaler()
66
+ X_train = scaler.fit_transform(X_train)
67
+ X_test = scaler.transform(X_test)
68
+
69
+ # Train Support Vector Machine Model
70
+ model = SVC(kernel='linear', probability=True, random_state=42)
71
+ model.fit(X_train, y_train)
72
+ y_pred = model.predict(X_test)
73
+
74
+ # Top Tabs Navigation
75
+ tab1, tab2, tab3 = st.tabs(["๐Ÿ“Š Dataset", "๐Ÿ“ˆ Visualization", "๐Ÿ”ฎ Prediction"])
76
+
77
+ # Dataset Section
78
+ with tab1:
79
+ st.write("### ๐Ÿ“Š Dataset Preview")
80
+ st.dataframe(df.head())
81
+
82
+ # Visualization Section
83
+ with tab2:
84
+ # Display model performance
85
+ accuracy = accuracy_score(y_test, y_pred)
86
+ st.write("### ๐Ÿ”ฅ Model Performance")
87
+ st.write(f"**โœ… Model Accuracy:** {accuracy:.2f}")
88
+
89
+ # Visualizing performance
90
+ st.write("### ๐Ÿ“Š Performance Breakdown")
91
+ conf_matrix = confusion_matrix(y_test, y_pred)
92
+ st.write("Confusion Matrix:")
93
+ fig, ax = plt.subplots()
94
+ sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='coolwarm', ax=ax)
95
+ st.pyplot(fig)
96
+
97
+ # Prediction Section
98
+ with tab3:
99
+ st.write("### ๐Ÿ”ฎ Predict Customer Churn")
100
+ st.markdown("Adjust the stats below to simulate a customer scenario!")
101
+
102
+ tenure = st.slider("Customer Tenure (Months)", min_value=0, max_value=72, value=12)
103
+ monthly_charges = st.slider("Monthly Charges ($)", min_value=0, max_value=200, value=50)
104
+ total_charges = st.slider("Total Charges ($)", min_value=0, max_value=10000, value=600)
105
+
106
+ if st.button("โœจ Predict Churn"):
107
+ input_data = scaler.transform([[tenure, monthly_charges, total_charges]])
108
+ prediction = model.predict(input_data)[0]
109
+ prediction_proba = model.predict_proba(input_data)[0]
110
+
111
+ st.subheader("๐Ÿ”ฎ Prediction Result")
112
+ result_text = "๐Ÿšจ Customer is likely to CHURN!" if prediction == 1 else "โœ… Customer is likely to STAY."
113
+ st.success(result_text) if prediction == 0 else st.error(result_text)
114
+ st.write(f"Confidence: {prediction_proba[prediction]:.2f}")
115
+
116
+ # Churn/Stay Bar Chart
117
+ st.write("### ๐Ÿ“Š Churn Probability Breakdown")
118
+ fig, ax = plt.subplots()
119
+ ax.bar(["Stay", "Churn"], [prediction_proba[0], prediction_proba[1]], color=["#64FFDA", "#FF4C4C"])
120
+ ax.set_ylim(0, 1)
121
+ ax.set_ylabel("Probability")
122
+ ax.set_title("Customer Churn Probability")
123
+ st.pyplot(fig)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ numpy
4
+ matplotlib
5
+ seaborn
6
+ scikit-learn