-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamlit_app.py
715 lines (533 loc) · 24.5 KB
/
Streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# In this program we will apply various ML algorithms to the built in datasets in scikit-learn
# Importing required Libraries
# Importing Numpy
import numpy as np
# To read csv file
import pandas as pd
# For splitting between training and testing
from sklearn.model_selection import train_test_split
# Importing Algorithm for Simple Vector Machine
from sklearn.svm import SVC
# Importing Knn algorithm
from sklearn.neighbors import KNeighborsClassifier
# Importing Decision Tree algorithm
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
# Importing Random Forest Classifer
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
# Importing Naive Bayes algorithm
from sklearn.naive_bayes import GaussianNB
# Importing Linear and Logistic Regression
from sklearn.linear_model import LogisticRegression
# Importing accuracy score
from sklearn.metrics import accuracy_score
# Importing PCA for dimension reduction
from sklearn.decomposition import PCA
# For Plotting
import matplotlib.pyplot as plt
import seaborn as sns
# For model deployment
import streamlit as st
# Reading Data
data = pd.read_csv("Preprocessed_Customer_Prediction.csv")
# Creating Multipage Streamlit Application for showing each stage
# <--------------------------------------------------- Step 1 Exploratory Data Analysis --------------------------------------------------->
def Step_1_EDA(df):
st.title("Exploratory Data Analysis")
st.write("Columns of Dataset are:\n", df.columns)
st.write("\nShape of Dataset is:", df.shape)
# Total Count of Datatypes
print("Datatype of Dataset is:\n")
df.info()
# According to Columns
data_types = df.dtypes
print("\nDatatypes of Columns is:\n")
print(data_types)
Numerical_Columns = ["tenure", "MonthlyCharges", "TotalCharges"]
st.subheader("Numerical Columns Plot")
# Plotting histograms and boxplots for numerical columns
for i in Numerical_Columns:
if df[i].dtype == "int64" or df[i].dtype == "float64":
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
# Plotting histogram
sns.histplot(df[i], kde=True, ax=axs[0])
# Adjusting font weight for axis labels, title, and ticks
axs[0].set_xlabel(i, fontweight="bold")
axs[0].set_ylabel("Count", fontweight="bold")
axs[0].set_title(f"Distribution of {i}", fontweight="bold")
plt.setp(axs[0].get_xticklabels(), fontweight="bold")
plt.setp(axs[0].get_yticklabels(), fontweight="bold")
sns.boxplot(df[i], ax=axs[1])
# Adjusting font weight for axis labels, title, and ticks
axs[1].set_xlabel(i, fontweight="bold")
axs[1].set_ylabel("Count", fontweight="bold")
axs[1].set_title(f"Distribution of {i}", fontweight="bold")
plt.setp(axs[1].get_xticklabels(), fontweight="bold")
plt.setp(axs[1].get_yticklabels(), fontweight="bold")
# Printing the plot in streamlit website
st.pyplot(fig)
# Plotting count plots for categorical columns
categorical_columns = [
"gender",
"SeniorCitizen",
"Partner",
"Dependents",
"PhoneService",
"MultipleLines",
"InternetService",
"OnlineSecurity",
"OnlineBackup",
"DeviceProtection",
"TechSupport",
"StreamingTV",
"StreamingMovies",
"Contract",
"PaperlessBilling",
"PaymentMethod",
]
st.subheader("Categorical Columns Plot")
fig, axs = plt.subplots(8, 2, figsize=(20, 40))
plt.subplots_adjust(hspace=0.5)
for i, col in enumerate(categorical_columns):
sns.countplot(x=df[col], data=df, ax=axs[i // 2, i % 2], hue="Churn")
# Adjusting font weight for axis labels, title, and ticks
axs[i // 2, i % 2].set_xlabel(col, fontweight="bold")
axs[i // 2, i % 2].set_ylabel("Count", fontweight="bold")
axs[i // 2, i % 2].set_title(f"Distribution of {col}", fontweight="bold")
plt.setp(axs[i // 2, i % 2].get_xticklabels(), fontweight="bold")
plt.setp(axs[i // 2, i % 2].get_yticklabels(), fontweight="bold")
# Fixing the legend box
axs[i // 2, i % 2].legend(loc="upper right", frameon=True, fontsize="large")
plt.tight_layout()
# Printing the plot in streamlit website
st.pyplot(fig)
# <------------------------------------------ Step 2 Feature Engineering------------------------------------------------------------------->
def Step_2_Feature_Engineering():
st.title("Feature Engineering")
st.info(
"Feature engineering is the process of transforming raw data into meaningful features that enhance the "
"performance of machine learning models, "
"often involving tasks such as dimensionality reduction, creating new features, and handling missing values."
)
st.subheader(
"Here in this dataset, customer Id column was removed as it was not useful"
)
st.subheader(
"After this the target Variable Churn class was highly imbalanced so it was balanced"
)
st.write(
"Value Counts of Target Column after Feature Engineering is:",
data["Churn"].value_counts(),
)
# <------------------------------------------ Step 3 Data Preprocessing --------------------------------------------------------------------->
def Step_3_Data_Preprocessing(df):
st.title("Data Preprocessing")
st.info(
"Data preprocessing involves transforming raw data into a clean, structured format suitable for analysis."
)
st.subheader("Here in this dataset, Encoding of Categorical Columns was done")
st.subheader(
"After this the normalization and Scaling was done for column monthly and total charges"
)
st.write("Descriptive Statistics of Dataset after Preprocessing is:", df.describe())
import warnings
warnings.filterwarnings("ignore")
corr_mat = df.corr()
st.write("\n")
st.subheader("Correlation Plot of Dataset\n\n")
# Setting Figure Size
fig = plt.figure(figsize=(19, 10))
sns.heatmap(corr_mat, annot=True, cmap="viridis")
plt.title(
"Heat Map/ Correlation Plot for Customer Churn Prediction Dataset",
fontweight="bold",
fontsize=22,
pad=20,
)
plt.xticks(fontweight="bold")
plt.yticks(fontweight="bold")
# Printing the plot in streamlit website
st.pyplot(fig)
# <------------------------------------------ Step 4 Model Development -------------------------------------------------------------------->
def Step_4_Model_Development():
st.title("Model Development")
st.info(
"Model development involves training and fine-tuning machine "
"learning algorithms to make predictions or classifications based on input data."
)
st.write("The Following Model were trained for this dataset:\n")
# List of models
models = [
"KNN",
"SVM",
"Decision Tree",
"Naive Bayes",
"Random Forest",
"Logistic Regression",
]
# One-liner descriptions for each model
descriptions = {
"KNN": "K-Nearest Neighbors is a simple, instance-based learning algorithm.",
"SVM": "Support Vector Machine is a powerful supervised learning algorithm.",
"Decision Tree": "Decision Tree is a versatile algorithm known for its interpretability.",
"Naive Bayes": "Naive Bayes is a probabilistic classifier based on Bayes' theorem.",
"Random Forest": "Random Forest is an ensemble learning method that builds multiple decision trees.",
"Logistic Regression": "Logistic Regression is a linear model used for binary classification.",
}
# Creating a dataframe from the list
df_models = pd.DataFrame(models, columns=["Models"], index=range(1, 7))
# Adding one-liner descriptions to the dataframe
df_models["Description"] = df_models["Models"].map(descriptions)
# Display the DataFrame as a Markdown table
# To successfully run this we need to install tabulate
st.markdown(df_models.to_markdown(index=False), unsafe_allow_html=True)
# <------------------------------------------ Step 5 Model Evaluation -------------------------------------------------------------------->
# Adding Parameters so that we can select from various parameters for classifier
def add_parameter_classifier(algorithm):
# Declaring a dictionary for storing parameters
params = dict()
# Deciding parameters based on algorithm
# Adding paramters for SVM
if algorithm == "SVM":
# Adding regularization parameter from range 0.01 to 10.0
c_regular = st.sidebar.slider("C (Regularization)", 0.01, 10.0)
# Kernel is the arguments in the ML model
# Polynomial ,Linear, Sigmoid and Radial Basis Function are types of kernals which we can add
kernel_custom = st.sidebar.selectbox(
"Kernel", ("linear", "poly", "rbf", "sigmoid")
)
# Adding in dictionary
params["C"] = c_regular
params["kernel"] = kernel_custom
if kernel_custom == "linear":
st.sidebar.info(
"SVM is Slow for this kernel as the dataset is very large.Try with other kernels speed will be improved."
)
# Adding Parameters for KNN
elif algorithm == "KNN":
# Adding number of Neighbour in Classifier
k_n = st.sidebar.slider("Number of Neighbors (K)", 1, 20)
# Adding in dictionary
params["K"] = k_n
# Adding weights
weights_custom = st.sidebar.selectbox("Weights", ("uniform", "distance"))
# Adding to dictionary
params["weights"] = weights_custom
elif algorithm == "Naive Bayes":
st.sidebar.info(
"This is a simple Algorithm. It doesn't have Parameters for Hyper-tuning."
)
# Adding Parameters for Decision Tree
elif algorithm == "Decision Tree":
# Taking max_depth
max_depth = st.sidebar.slider("Max Depth", 2, 17)
# Adding criterion
criterion = st.sidebar.selectbox("Criterion", ("gini", "entropy"))
# Adding splitter
splitter = st.sidebar.selectbox("Splitter", ("best", "random"))
# Taking random state
# Adding to dictionary
params["max_depth"] = max_depth
params["criterion"] = criterion
params["splitter"] = splitter
# Exception Handling using try except block
# Because we are sending this input in algorithm model it will show error before any input is entered
# For this we will do a default random state till the user enters any state and after that it will be updated
try:
random = st.sidebar.text_input("Enter Random State")
params["random_state"] = int(random)
except:
params["random_state"] = 4567
# Adding Parameters for Random Forest
elif algorithm == "Random Forest":
# Taking max_depth
max_depth = st.sidebar.slider("Max Depth", 2, 17)
# Adding number of estimators
n_estimators = st.sidebar.slider("Number of Estimators", 1, 9)
# Adding criterion
# mse is for regression- It is used in RandomForestRegressor
# mse will give error in classifier so it is removed
criterion = st.sidebar.selectbox("Criterion", ("gini", "entropy", "log_loss"))
# Adding to dictionary
params["max_depth"] = max_depth
params["n_estimators"] = n_estimators
params["criterion"] = criterion
# Exception Handling using try except block
# Because we are sending this input in algorithm model it will show error before any input is entered
# For this we will do a default random state till the user enters any state and after that it will be updated
try:
random = st.sidebar.text_input("Enter Random State")
params["random_state"] = int(random)
except:
params["random_state"] = 4567
# Adding Parameters for Logistic Regression
else:
# Adding regularization parameter from range 0.01 to 10.0
c_regular = st.sidebar.slider("C (Regularization)", 0.01, 10.0)
params["C"] = c_regular
# Taking fit_intercept
fit_intercept = st.sidebar.selectbox("Fit Intercept", ("True", "False"))
params["fit_intercept"] = bool(fit_intercept)
# Taking Penalty only l2 and None is supported
penalty = st.sidebar.selectbox("Penalty", ("l2", None))
params["penalty"] = penalty
# Taking n_jobs
n_jobs = st.sidebar.selectbox("Number of Jobs", (None, -1))
params["n_jobs"] = n_jobs
return params
# Now we will build ML Model for this dataset and calculate accuracy for that for classifier
def model_classifier(algorithm, params):
if algorithm == "KNN":
return KNeighborsClassifier(n_neighbors=params["K"], weights=params["weights"])
elif algorithm == "SVM":
return SVC(C=params["C"], kernel=params["kernel"])
elif algorithm == "Decision Tree":
return DecisionTreeClassifier(
criterion=params["criterion"],
splitter=params["splitter"],
random_state=params["random_state"],
)
elif algorithm == "Naive Bayes":
return GaussianNB()
elif algorithm == "Random Forest":
return RandomForestClassifier(
n_estimators=params["n_estimators"],
max_depth=params["max_depth"],
criterion=params["criterion"],
random_state=params["random_state"],
)
else:
return LogisticRegression(
fit_intercept=params["fit_intercept"],
penalty=params["penalty"],
C=params["C"],
n_jobs=params["n_jobs"],
)
# Now we will write the dataset information
def info(data_name, algorithm, X, Y):
st.write(f"## Classification {data_name} Dataset")
st.write(f'Algorithm is : {algorithm + " " + "Classifier"}')
# Printing shape of data
st.write("Shape of Dataset is: ", X.shape)
st.write("Number of classes: ", len(np.unique(Y)))
# Making a dataframe to store target name and value
df = pd.DataFrame(
{"Target Value": list(np.unique(Y)), "Target Name": ["Not Churn", "Churn"]}
)
# Display the DataFrame without index labels
st.write("Values and Name of Classes")
# Display the DataFrame as a Markdown table
# To successfully run this we need to install tabulate
st.markdown(df.to_markdown(index=False), unsafe_allow_html=True)
st.write("\n")
def Step_5_Model_Evaluation(data):
# Giving Title
st.title("Model Evaluation")
# Now we are making a select box for dataset
data_name = "Customer Churn Prediction"
# The Next is selecting algorithm
# We will display this in the sidebar
algorithm = st.sidebar.selectbox(
"Select Supervised Learning Algorithm",
(
"KNN",
"SVM",
"Decision Tree",
"Naive Bayes",
"Random Forest",
"Logistic Regression",
),
)
# Now after this we need to split between input and output
# Defining Input and Output
# Separating as input and output
X, Y = data.drop(["Churn"], axis=1), data["Churn"]
# Now splitting into Testing and Training data
# It will split into 80 % training data and 20 % Testing data
x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.8)
# Ensuring data contiguity using np.ascontiguousarray() to resolve the 'c_contiguous' attribute issue
# This is done because certain scikit learn function expect contiguous data
x_train = np.ascontiguousarray(x_train)
y_train = np.ascontiguousarray(y_train)
x_test = np.ascontiguousarray(x_test)
y_test = np.ascontiguousarray(y_test)
# Calling Function based on algorithm
params = add_parameter_classifier(algorithm)
# Choosing algorithm
# Calling Function based on classifier
algo_model = model_classifier(algorithm, params)
# Calling function to print Dataset Information
info(data_name, algorithm, X, Y)
# Training algorithm
algo_model.fit(x_train, y_train)
# Now we will find the predicted values
predict = algo_model.predict(x_test)
# Finding Accuracy
# Evaluating/Testing the model
# For all algorithm we will find accuracy
st.write("Training Accuracy is:", algo_model.score(x_train, y_train) * 100)
st.write("Testing Accuracy is:", accuracy_score(y_test, predict) * 100)
# Plotting Dataset
# Since there are many dimensions, first we will do Principle Component analysis to do dimension reduction and then plot
# Doing PCA for dimension reduction
pca = PCA(3)
x = pca.fit(x_test).transform(x_test)
print("Transformed Data is:\n", x)
print("\nShape of Transformed data is:", x.shape)
# Plotting dataset
fig = plt.figure()
colors = ["lightblue", "orange"]
# Adjusting alpha for transparency as there are a lot of overlapping points in the dataset
sns.scatterplot(
x=x[:, 0], y=x[:, 1], hue=predict, palette=sns.color_palette(colors), alpha=0.4
)
# Adding x and y labels
plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")
# Giving Title
plt.title("Scatter Classification Plot of Dataset With Target Classes")
# Printing the plot in streamlit website
st.pyplot(fig)
# <------------------------------------------ Step 6 Predictions -------------------------------------------------------------------->
def Step_6_Predictions(data):
st.title("Predictions")
st.info(
"After Finding the best model we can give it to model and it will generate the target variable"
)
st.write(
"This code snippet selects a specific tuple (row) from a DataFrame for prediction, drops the target "
"variable column from the selected tuple, reshapes it to match the model's input requirements, and "
"then predicts using the reshaped tuple. "
"Finally, it prints the prediction and the actual target variable value for the sample tuple."
)
# Now after this we need to split between input and output
# Defining Input and Output
# Separating as input and output
X, Y = data.drop(["Churn"], axis=1), data["Churn"]
# Now splitting into Testing and Training data
# It will split into 80 % training data and 20 % Testing data
x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.8)
# Ensuring data contiguity using np.ascontiguousarray() to resolve the 'c_contiguous' attribute issue
# This is done because certain scikit learn function expect contiguous data
x_train = np.ascontiguousarray(x_train)
y_train = np.ascontiguousarray(y_train)
x_test = np.ascontiguousarray(x_test)
y_test = np.ascontiguousarray(y_test)
from sklearn.ensemble import RandomForestClassifier
# Define the best parameters
best_params = {
"criterion": "gini",
"max_depth": 10,
"min_samples_leaf": 4,
"min_samples_split": 10,
"n_estimators": 200,
}
# Initialize the RandomForestClassifier with best parameters
rf_classifier = RandomForestClassifier(**best_params)
# Train the classifier using input data X and target variable Y
rf_classifier.fit(X, Y)
# Input box to take index input from the user
index = st.text_input("Enter the index of the row you want to predict:", "")
# Check if the index is provided by the user
if index:
# Convert index to integer
index = int(index)
# Select the row from the DataFrame using the index
sample_tuple = data.iloc[index]
# Drop the target variable column from the selected tuple
X_sample = sample_tuple.drop("Churn")
# Reshape the sample tuple to match the model's input requirements
X_sample_reshaped = X_sample.values.reshape(1, -1)
# Make prediction using the sample tuple
prediction = rf_classifier.predict(X_sample_reshaped)
# Print the Actual Value
st.write("Actual Value:", data.iloc[index]["Churn"])
# Print the prediction
st.write("Prediction:", prediction)
# <------------------------------------------ Step 7 Recommendations -------------------------------------------------------------------->
def Step_7_Recommendations():
st.title("Recommendations for Reducing Customer Churn")
st.markdown("### 1. Senior Citizen Benefits")
st.markdown(
"- As observed in the dataset, there is a lower usage among senior citizens, and the retention rate is also low. To increase customer retention, special benefits such as discounts, messages, or tech support can be provided to senior citizens."
)
st.markdown("### 2. Single User Benefits")
st.markdown(
"- More single users (without partners) are observed in the dataset. Providing special benefits like discounts or tech support tailored for single users can help in retaining them."
)
st.markdown("### 3. Events")
st.markdown(
"- Organizing informational events can help users utilize services more efficiently. Direct conversations with users may be more effective than emails."
)
st.markdown("### 4. Feedback")
st.markdown(
"- Collecting customer feedback can provide insights into the root causes of dissatisfaction. Conducting dissatisfaction analysis is crucial."
)
st.markdown("### 5. More Focus on Long-term Users")
st.markdown(
"- Users with a longer association with the company are more valuable. Offering extra benefits to users who have been using the service for an extended period, like 30-40 months, can reduce churn."
)
st.markdown("### 6. Utilizing ML Modeling")
st.markdown(
"- ML models can predict which users are more likely to churn. Providing additional benefits to such users based on model predictions can help reduce churn."
)
# Function to include background image and opacity
def display_background_image(url, opacity):
"""
Displays a background image with a specified opacity on the web app using CSS.
Args:
- url (str): URL of the background image.
- opacity (float): Opacity level of the background image.
"""
# Set background image using HTML and CSS
st.markdown(
f"""
<style>
body {{
background: url('{url}') no-repeat center center fixed;
background-size: cover;
opacity: {opacity};
}}
</style>
""",
unsafe_allow_html=True,
)
# Main Function
if __name__ == "__main__":
# Setting the page title
# This title will only be visible when running the app locally.
# In the deployed app, the title will be displayed as "Title - Streamlit," where "Title" is the one we provide.
# If we don't set the title, it will default to "Streamlit"
st.set_page_config(page_title="Customer Churn")
# Call function to display the background image with opacity
display_background_image(
"https://emyrael.github.io/assets/img/churn.png",
0.8,
)
# Define navigation components
nav = st.sidebar.radio(
"Step Navigation",
[
"Exploratory Data Analysis",
"Feature Engineering",
"Data Preprocessing",
"Model Development",
"Model Evaluation",
"Predictions",
"Recommendations",
],
)
# Render page content based on navigation
if nav == "Exploratory Data Analysis":
Step_1_EDA(data)
elif nav == "Feature Engineering":
Step_2_Feature_Engineering()
elif nav == "Data Preprocessing":
Step_3_Data_Preprocessing(data)
elif nav == "Model Development":
Step_4_Model_Development()
elif nav == "Model Evaluation":
Step_5_Model_Evaluation(data)
elif nav == "Predictions":
Step_6_Predictions(data)
elif nav == "Recommendations":
Step_7_Recommendations()