|
| 1 | +import streamlit as st |
| 2 | +import pickle |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +from sklearn.preprocessing import StandardScaler |
| 6 | + |
| 7 | +# Load the LabelEncoder |
| 8 | +with open('encoder.pkl', 'rb') as f: |
| 9 | + encoder = pickle.load(f) |
| 10 | + |
| 11 | +# Load the StandardScaler |
| 12 | +with open('standard_scaler.pkl', 'rb') as f: |
| 13 | + scaler = pickle.load(f) |
| 14 | + |
| 15 | +# Load the SVC model |
| 16 | +with open('SVC.pkl', 'rb') as f: |
| 17 | + model = pickle.load(f) |
| 18 | + |
| 19 | +# Define the UI |
| 20 | +st.set_page_config(page_title="Customer Churn Prediction", layout="wide") |
| 21 | +st.header("Customer Churn Prediction App") |
| 22 | +st.write("This app predicts whether a customer is likely to churn based on their demographic and banking information.") |
| 23 | + |
| 24 | +# Define the categorical feature mappings |
| 25 | +categorical_mappings = { |
| 26 | + 'Geography': ['France', 'Germany', 'Spain'], |
| 27 | + 'Gender': ['Female', 'Male'], |
| 28 | + 'Has Credit Card': ['No', 'Yes'], |
| 29 | + 'Is Active Member': ['No', 'Yes'] |
| 30 | +} |
| 31 | + |
| 32 | +# Input widgets for categorical features |
| 33 | +col1, col2 = st.columns(2) |
| 34 | +selected_category = {feature: col1.selectbox(f'{feature}', options, index=None) for feature, options in list(categorical_mappings.items())} |
| 35 | + |
| 36 | +# Input widgets for numerical features |
| 37 | +col3, col4, col5, col6, col7, col8 = st.columns(6) |
| 38 | +creditscore = col3.slider('Credit Score', 300, 850, 500) |
| 39 | +age = col4.slider('Age', 18, 100, 30) |
| 40 | +tenure = col5.slider('Tenure', 0, 10, 5) |
| 41 | +balance = col6.number_input('Balance', min_value=0.0, max_value=250000.0, step=1000.0, value=0.0) |
| 42 | +num_of_products = col7.slider('Number of Products', 1, 4, 2) |
| 43 | +estimated_salary = col8.number_input('Estimated Salary', min_value=0.0, max_value=1000000.0, step=1000.0, value=0.0) |
| 44 | + |
| 45 | +# Map 'Yes'/'No' to 1/0 for 'Has Credit Card' and 'Is Active Member' |
| 46 | +has_cr_card = 1 if selected_category['Has Credit Card'] == 'Yes' else 0 |
| 47 | +is_active_member = 1 if selected_category['Is Active Member'] == 'Yes' else 0 |
| 48 | + |
| 49 | +# Button to predict |
| 50 | +if st.button('Predict'): |
| 51 | + if None in selected_category.values(): |
| 52 | + missing_categories = [key for key, value in selected_category.items() if value is None] |
| 53 | + missing_categories_str = ", ".join(missing_categories) |
| 54 | + st.error(f"Please select values for the following categorical features: {missing_categories_str}") |
| 55 | + else: |
| 56 | + # Encode categorical features |
| 57 | + encoded_features = [] |
| 58 | + for feature, value in selected_category.items(): |
| 59 | + # Skip 'Has Credit Card' and 'Is Active Member' |
| 60 | + if feature in ['Has Credit Card', 'Is Active Member']: |
| 61 | + continue |
| 62 | + # Create a DataFrame with the selected category |
| 63 | + df = pd.DataFrame({feature: [value]}) |
| 64 | + # One-hot encode the categorical feature |
| 65 | + encoded_df = pd.get_dummies(df, columns=[feature], drop_first=True) |
| 66 | + # Convert DataFrame to numpy array |
| 67 | + encoded_value = encoded_df.values |
| 68 | + # Append the encoded value |
| 69 | + encoded_features.append(encoded_value) |
| 70 | + |
| 71 | + # Scale numerical features |
| 72 | + numerical_features = [creditscore, age, tenure, balance, num_of_products, estimated_salary] |
| 73 | + scaled_features = scaler.transform([numerical_features]) |
| 74 | + |
| 75 | + # Combine encoded categorical features and scaled numerical features |
| 76 | + input_data = np.concatenate(encoded_features + [scaled_features], axis=1) |
| 77 | + |
| 78 | + # Append 'Has Credit Card' and 'Is Active Member' |
| 79 | + input_data = np.append(input_data, [[has_cr_card, is_active_member]], axis=1) |
| 80 | + |
| 81 | + # Make prediction |
| 82 | + prediction = model.predict(input_data)[0] |
| 83 | + prediction_text = 'Yes' if prediction == 1 else 'No' |
| 84 | + if prediction_text == 'Yes': |
| 85 | + styled_prediction = '<span style="font-size: 20px; color: green; font-weight: bold;">Yes</span>' |
| 86 | + else: |
| 87 | + styled_prediction = '<span style="font-size: 20px; color: red; font-weight: bold;">No</span>' |
| 88 | + |
| 89 | + # Display prediction with a subheader |
| 90 | + st.subheader("Prediction Result") |
| 91 | + st.markdown(f"The Customer is likely to churn: {styled_prediction}", unsafe_allow_html=True) |
| 92 | + |
0 commit comments