saikiranmansa commited on
Commit
3ace4c1
·
verified ·
1 Parent(s): d3daaaf

Upload 4 files

Browse files
Files changed (4) hide show
  1. BankChurners.csv +0 -0
  2. app.py +101 -0
  3. bank churn prediction.ipynb +0 -0
  4. model.pkl +3 -0
BankChurners.csv ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import pickle
4
+ import numpy as np
5
+
6
+ st.markdown("<h1 style='text-align: center; font-size: 48px; color: red;'>Bank Customer Churn Prediction </h1>", unsafe_allow_html=True)
7
+
8
+ # Load the dataset
9
+ @st.cache_data
10
+ def load_dataset():
11
+ return pd.read_csv('BankChurners.csv') # Update with the path to your dataset
12
+
13
+ # Cache function to convert DataFrame to CSV
14
+ @st.cache_data
15
+ def convert_df(df):
16
+ return df.to_csv(index=False).encode("utf-8")
17
+
18
+ # Load pre-trained model
19
+ @st.cache_resource
20
+ def load_model():
21
+ with open("model.pkl", "rb") as f:
22
+ model = pickle.load(f)
23
+ return model
24
+
25
+ # Load the dataset
26
+ df = load_dataset()
27
+
28
+ # Display the dataset preview
29
+ st.write("Dataset Preview:")
30
+ st.dataframe(df.head())
31
+
32
+ # Ensure the dataset has the required columns
33
+ required_columns = ['CLIENTNUM', 'Total_Trans_Ct', 'Total_Ct_Chng_Q4_Q1', 'Total_Revolving_Bal',
34
+ 'Avg_Utilization_Ratio', 'Total_Trans_Amt', 'Total_Relationship_Count',
35
+ 'Total_Amt_Chng_Q4_Q1', 'Gender', 'Credit_Limit', 'Card_Category', 'Avg_Open_To_Buy']
36
+
37
+ if not all(col in df.columns for col in required_columns):
38
+ st.error(f"The dataset must contain the following columns: {', '.join(required_columns)}")
39
+ else:
40
+ # Add functionality for row selection
41
+ st.markdown("### Select a Customer Row for Prediction:")
42
+
43
+ # Option to select a row by index
44
+ selected_row_index = st.selectbox("Select a Row Index", options=range(len(df)), index=0)
45
+
46
+ # Add a button below the row selection for churn prediction
47
+ predict_button = st.button("Predict Churn")
48
+
49
+ # If the "Predict" button is clicked
50
+ if predict_button:
51
+ # Row to use for the model
52
+ row_to_use = df.iloc[selected_row_index]
53
+
54
+ # Prepare input data for the model
55
+ gender_mapping = {"M": 0, "F": 1}
56
+ card_category_mapping = {"Blue": 0, "Gold": 1, "Platinum": 2, "Titanium": 3}
57
+
58
+ # Map categorical values to numerical ones
59
+ row_to_use_for_model = [
60
+ row_to_use['CLIENTNUM'],
61
+ row_to_use['Total_Trans_Ct'],
62
+ row_to_use['Total_Ct_Chng_Q4_Q1'],
63
+ row_to_use['Total_Revolving_Bal'],
64
+ row_to_use['Avg_Utilization_Ratio'],
65
+ row_to_use['Total_Trans_Amt'],
66
+ row_to_use['Total_Relationship_Count'],
67
+ row_to_use['Total_Amt_Chng_Q4_Q1'],
68
+ gender_mapping[row_to_use['Gender']],
69
+ row_to_use['Credit_Limit'],
70
+ card_category_mapping[row_to_use['Card_Category']],
71
+ row_to_use['Avg_Open_To_Buy']
72
+ ]
73
+
74
+ # Load the model
75
+ model = load_model()
76
+
77
+ # Check if the number of features matches the model's expectations
78
+ if len(row_to_use_for_model) != model.n_features_in_:
79
+ st.error(f"The model expects {model.n_features_in_} features, but {len(row_to_use_for_model)} were provided.")
80
+ else:
81
+ # Apply the model for churn prediction
82
+ prediction = model.predict([row_to_use_for_model])
83
+
84
+ # Display the row and the churn prediction result
85
+ st.write("Row selected for churn prediction:")
86
+ st.write(row_to_use)
87
+
88
+ # Show the prediction result
89
+ result = "Likely to Churn" if prediction[0] == 1 else "Likely to Stay"
90
+ st.write(f"Churn Prediction Result: {result}")
91
+
92
+ # Provide option to download the result
93
+ result_df = row_to_use.to_frame().T # Convert Series to DataFrame
94
+ result_df['Churn Prediction'] = result
95
+ result_csv = convert_df(result_df)
96
+ st.download_button(
97
+ label="Download Prediction Result",
98
+ data=result_csv,
99
+ file_name="Churn_Prediction_Result.csv",
100
+ mime="text/csv",
101
+ )
bank churn prediction.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc592f32d00a419f2962d0ef2eaaf82a584b4536b01421b5573264f7a3d952ad
3
+ size 5746681