import os
import time
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
from PIL import Image
from utils import load_data_pickle, load_model_pickle
from st_pages import add_indentation
from annotated_text import annotated_text
#####################################################################################
# PAGE CONFIG
#####################################################################################
#add_indentation()
st.set_page_config(layout="wide")
#####################################################################################
# INTRO
#####################################################################################
#st.image("images/ML_header.jpg", use_column_width=True)
st.markdown("# Supervised vs Unsupervised Learning π")
st.info("""There are two main types of models in the field of Data Science, **Supervised** and **Unsupervised learning** models.
Being able to distinguish which type of model fits your data is an essential step in building any AI project.""")
st.markdown(" ")
#st.markdown("## What are the differences between both ?")
col1, col2 = st.columns(2, gap="large")
with col1:
st.markdown("## Supervised Learning")
st.markdown("""Supervised learning models are trained by learning from **labeled data**.
Labeled data provides to the model the desired output, which it will then use to learn relevant patterns and make predictions.
- A model is first **trained** to make predictions using labeled data.
- The trained model can then be used to **predict values** for new data.
""", unsafe_allow_html=True)
st.markdown(" ")
st.image("images/supervised_learner.png", caption="An example of supervised learning")
with col2:
st.markdown("## Unsupervised Learning")
st.markdown("""Unsupervised learning models learn the data's inherent structure without any explicit guidance on what to look for.
The algorithm will identify any naturally occurring patterns in the dataset using **unlabeled data**.
- They can be useful for applications where the goal is to discover **unknown groupings** in the data.
- They are also used to identify unusual patterns or **outliers**.
""", unsafe_allow_html=True)
st.markdown(" ")
st.image("images/unsupervised_learning.png", caption="An example of unsupervised Learning",
use_column_width=True)
st.markdown(" ")
learning_type = st.selectbox("**Select a type of model**",
["Supervised Learning",
"Unsupervised Learning"])
#######################################################################################################################
# SUPERVISED LEARNING
#######################################################################################################################
if learning_type == "Supervised Learning":
sl_usecase = st.selectbox("**Choose a use case**",
["Credit score classification π―",
"Customer churn prediction β"])
st.markdown(" ")
# st.divider()
path_data_supervised = r"data/classification"
path_pretrained_supervised = r"pretrained_models/supervised_learning"
################################# CREDIT SCORE ######################################
if sl_usecase == "Credit score classification π―":
path_credit = os.path.join(path_data_supervised,"credit_score")
## Description of the use case
st.divider()
st.markdown("# Credit score classification π―")
st.info("""**Classification** is a type of supervised learning where the goal is to categorize input data into predefined classes or categories.
In this case, we will build a **credit score classification** model that predicts if a client will have a **'Bad'**, **'Standard'** or **'Good'** credit score.""")
st.markdown(" ")
_, col, _ = st.columns([0.25,0.5,0.25])
with col:
st.image("images/credit_score.jpg")
## Learn about the data
st.markdown("#### About the data π")
st.markdown("""To train the credit classification model, you were provided a **labeled** database with the bank and credit-related information of around 7600 clients.
This dataset is 'labeled' since it contains information on what we are trying to predict, which is the **Credit_Score** variable.""",
unsafe_allow_html=True)
## Load data
credit_train = load_data_pickle(path_credit, "credit_score_train_raw.pkl")
credit_test_pp = load_data_pickle(path_credit, "credit_score_test_pp.pkl")
labels = ["Good","Poor","Standard"]
## Load model
credit_model = load_model_pickle(path_pretrained_supervised,"credit_score_model.pkl")
# View data
see_data = st.checkbox('**See the data**', key="credit_score\data")
if see_data:
st.warning("The data of only the first 30 clients are shown.")
st.dataframe(credit_train.head(30).reset_index(drop=True))
learn_data = st.checkbox('**Learn more about the data**', key="credit_score_var")
if learn_data:
st.markdown("""
- **Age**: The client's age
- **Occupation**: The client's occupation/job
- **Credit_Mix**: Score for the different type of credit accounts a client has (mortgages, loans, credit cards, ...)
- **Payment_of_Min_Amount**: Whether the client is making the minimum required payments on their credit accounts (Yes, No, NM:Not mentioned)
- **Annual_Income**: The client's annual income
- **Num_Bank_Accounts**: Number of bank accounts opened
- **Num_Credit_Card**: Number of credit cards owned
- **Interest_Rate**: The client's average interest rate
- **Num_of_Loan**: Number of loans of the client
- **Changed_Credit_Limit**: Whether a client changed his credit limit once or not (Yes, No) -
- **Outstanding Debt**: A client's outstanding debt
- **Credit_History_Age**: The length of a client's credit history (in months)
""")
st.markdown(" ")
st.markdown(" ")
## Train the algorithm
st.markdown("#### Train the algorithm βοΈ")
st.info("""**Training** an AI model means feeding it data that contains multiple examples of clients with their credit scores.
Using the labeled data provided, the model will **learn relationships** between a client's credit score and the other bank/credit-related variables provided.
Using these learned relationships, the model will then try to make **accurate predictions**.""")
# st.markdown("""Before feeding the model data for training, exploratory data analysis is often conducted to discover if patterns can discovered beforehand.""")
# st.image("images/models/credit_score/EDA_numeric_credit.png")
#st.markdown("In our case, the training data is the dataset containing the bank and credit information of our 7600 customers.")
if 'model_train' not in st.session_state:
st.session_state['model_train'] = False
if st.session_state.model_train:
st.write("The model has been trained.")
else:
st.write("The model hasn't been trained yet")
run_credit_model = st.button("**Train the model**")
if run_credit_model:
st.session_state.model_train = True
with st.spinner('Wait for it...'):
st.markdown(" ")
st.markdown(" ")
time.sleep(2)
st.markdown("#### See the results βοΈ")
tab1, tab2 = st.tabs(["Performance", "Explainability"])
######## MODEL PERFORMANCE
with tab1:
results_train = load_data_pickle(path_credit,"credit_score_cm_train")
results_train = results_train.to_numpy()
accuracy = np.round(results_train.diagonal()*100)
df_accuracy = pd.DataFrame({"Credit Score":["Good","Poor","Standard"],
"Accuracy":accuracy})
st.markdown(" ")
st.info("""**Evaluating a model's performance** helps provide a quantitative measure of the model's ability to make accurate decisions.
In this use case, the performance of the credit score model was measured by comparing clients' true credit scores with the scores predicted by the trained model.""")
fig = px.bar(df_accuracy, y='Accuracy', x='Credit Score', color="Credit Score", title="Model performance")
st.plotly_chart(fig, use_container_width=True)
st.markdown("""The model's accuracy was measured for every type of credit score (Good, Standard, Poor).
This is crucial as to understand whether the model is consistant in its performance, or whether it has trouble distinguishing between two kinds of credit score.""",
unsafe_allow_html=True)
st.markdown(" ")
st.markdown("""**Interpretation**:
Our model's is overall quite accurate in predicting all types of credit scores with an accuracy that is above 85% for each.
We do note that is slighly more accuracte in predicting a good credit score (92%) and less for a standard credit score (86%).
This can be due to the model having a harder time distinguishing between clients with a standard credit score and other more "extreme" credit scores (Good, Bad).
""", unsafe_allow_html=True)
##### MODEL EXPLAINABILITY
with tab2:
st.markdown(" ")
st.info("""**Explainability** in AI refers to the ability to understand which variable used by a model during training had the most impact on the final predictions and how to quantify this impact.
Understanding the inner workings of a model helps build trust among users and stakeholders, as well as increase acceptance.""")
# Create feature importance dataframe
df_var_importance = pd.DataFrame({"variable":credit_test_pp.columns,
"score":credit_model.feature_importances_})
# Compute average score for categorical variables
for column in ["Occupation","Credit_Mix","Payment_of_Min_Amount"]:
col_remove = [col for col in credit_test_pp.columns if f"{column}_" in col]
avg_score = df_var_importance.loc[df_var_importance["variable"].isin(col_remove)]["score"].mean()
df_var_importance = df_var_importance.loc[~df_var_importance["variable"].isin(col_remove)]
new_row = pd.DataFrame([[column, avg_score]], columns=["variable","score"])
df_var_importance = pd.concat([df_var_importance, new_row], ignore_index=True)
df_var_importance.sort_values(by=["score"], inplace=True)
df_var_importance["score"] = df_var_importance["score"].round(3)
# Feature importance plot with plotly
fig = px.bar(df_var_importance, x='score', y='variable', color="score", orientation="h", title="Model explainability")
st.plotly_chart(fig, use_container_width=True)
st.markdown("""Interpretation:
A client's outstanding debt, interest rate and delay from due date were the most crucial factors in explaining their final credit score.
Whether a client is making their minimum required payments on their credit accounts (Payment_min_amount), their occupation and their number of loans had a very limited impact on their credit score.,
""", unsafe_allow_html=True)
st.markdown(" ")
st.markdown(" ")
## Make predictions
st.markdown("#### Predict credit score π")
st.info("You can only predict the credit score of new clients once the **model has been trained.**")
st.markdown(" ")
col1, col2 = st.columns([0.25,0.75], gap="medium")
credit_test = load_data_pickle(path_credit,"credit_score_test_raw.pkl")
credit_test.reset_index(drop=True, inplace=True)
credit_test.insert(0, "Client ID", [f"{i}" for i in range(credit_test.shape[0])])
credit_test = credit_test.loc[credit_test["Num_Bank_Accounts"]>0]
#credit_test.drop(columns=["Credit_Score"], inplace=True)
with col1:
st.markdown("""Filter the data
You can select clients based on their *Age*, *Annual income* or *Oustanding Debt*.""",
unsafe_allow_html=True)
select_image_box = st.radio(" ",
["Filter by Age", "Filter by Income", "Filter by Outstanding Debt", "No filters"],
label_visibility="collapsed")
if select_image_box == "Filter by Age":
st.markdown(" ")
min_age, max_age = st.slider('Select a range', credit_test["Age"].astype(int).min(), credit_test["Age"].astype(int).max(), (19,50),
key="age", label_visibility="collapsed")
credit_test = credit_test.loc[credit_test["Age"].between(min_age,max_age)]
if select_image_box == "Filter by Income":
st.markdown(" ")
min_income, max_income = st.slider('Select a range', credit_test["Annual_Income"].astype(int).min(), 180000,
(7000, 100000), label_visibility="collapsed", key="income")
credit_test = credit_test.loc[credit_test["Annual_Income"].between(min_income, max_income)]
if select_image_box == "Filter by Outstanding Debt":
min_debt, max_debt = st.slider('Select a range', credit_test["Outstanding_Debt"].astype(int).min(), credit_test["Outstanding_Debt"].astype(int).max(),
(0,2000), label_visibility="collapsed", key="debt")
credit_test = credit_test.loc[credit_test["Outstanding_Debt"].between(min_debt, max_debt)]
if select_image_box == "No filters":
pass
st.markdown(" ")
st.markdown("""Select a threshold for the alert
A warning message will be displayed if the percentage of poor credit scores exceeds this threshold.
""", unsafe_allow_html=True)
warning_threshold = st.slider('Select a value', min_value=20, max_value=100, step=10,
label_visibility="collapsed", key="warning")
st.markdown(" ")
st.write("The threshold is at", warning_threshold, "%")
with col2:
#st.markdown("**View the database**")
st.dataframe(credit_test)
make_predictions = st.button("**Make predictions**")
st.markdown(" ")
if make_predictions:
if st.session_state.model_train is True:
X_test = credit_test_pp.iloc[credit_test.index,:]
predictions = credit_model.predict(X_test)
df_results_pred = credit_test.copy()
df_results_pred["Credit Score"] = predictions
df_mean_pred = df_results_pred["Credit Score"].value_counts().to_frame().reset_index()
df_mean_pred.columns = ["Credit Score", "Proportion"]
df_mean_pred["Proportion"] = (100*df_mean_pred["Proportion"]/df_results_pred.shape[0]).round()
perct_bad_score = df_mean_pred.loc[df_mean_pred["Credit Score"]=="Poor"]["Proportion"].to_numpy()
if perct_bad_score >= warning_threshold:
st.error(f"The proportion of clients with a bad credit score is above {warning_threshold}% (at {perct_bad_score[0]}%)β οΈ")
col1, col2 = st.columns([0.4,0.6], gap="large")
with col1:
st.markdown("**Proporition of predicted credit scores**")
fig = px.pie(df_mean_pred, values='Proportion', names='Credit Score')
#title="Proportion of credit scores")
st.plotly_chart(fig, use_container_width=True)
with col2:
df_show_results = df_results_pred[["Credit Score","Client ID"] + [col for col in df_results_pred.columns if col not in ["Client ID","Credit Score"]]]
columns_float = df_show_results.select_dtypes(include="float").columns
df_show_results[columns_float] = df_show_results[columns_float].astype(int)
def highlight_score(val):
if val == "Good":
color = 'red'
if val == 'Standard':
color= "cornflowerblue"
if val == "Poor":
color = 'blue'
return f'color: {color}'
df_show_results_color = df_show_results.style.applymap(highlight_score, subset=['Credit Score'])
st.markdown("**Overall results**")
st.dataframe(df_show_results_color)
else:
st.error("You have to train the credit score model first.")
################################# CUSTOMER CHURN #####################################
elif sl_usecase == "Customer churn prediction β":
#st.warning("This page is under construction")
path_churn = r"data/classification/churn"
## Description of the use case
st.divider()
st.markdown("# Customer churn prediction β")
st.info(""" Classification is a type of supervised learning model whose goal is to categorize input data into predefined classes or categories.
In this example, we will build a **customer churn classification model** that can predict whether a customer is likely to leave a company's service in the future using historical data.
""")
st.markdown(" ")
## Load data
churn_data = load_data_pickle(path_churn, "churn_train_raw.pkl")
_, col, _ = st.columns([0.1,0.8,0.1])
with col:
st.image("images/customer-churn.png", use_column_width=True)
st.markdown(" ")
## Learn about the data
st.markdown("#### About the data π")
st.markdown("""To train the customer churn classification model, you were provided a **labeled** database with around 7000 clients of a telecommunications company.
The data contains information on which services the customer has signed for, information on his account as well as whether the customer churned or not (our label here).""",
unsafe_allow_html=True)
# st.markdown("This dataset is 'labeled' since it contains information on what we are trying to predict, which is the **Churn** variable.")
st.info("**Note**: The variables that had two possible values (Yes or No) where transformed into binary variables (0 or 1) with 0 being 'No' and 1 being 'Yes'.")
see_data = st.checkbox('**See the data**', key="churn-data")
if see_data:
st.warning("You can only view the first 30 customers in this section.")
churn_data = load_data_pickle(path_churn, "churn_train_raw.pkl")
st.dataframe(churn_data)
learn_data = st.checkbox('**Learn more about the data**', key="churn-var")
if learn_data:
st.markdown("""
- **SeniorCitizen**: Whether the customer is a senior citizen or not (1, 0)
- **Partner**: Whether the customer has a partner or not (Yes, No)
- **Dependents**: Whether the customer has dependents or not (Yes, No)
- **tenure**: Number of months the customer has stayed with the company
- **PhoneService**: Whether the customer has a phone service or not (Yes, No)
- **MultipleLines**: Whether the customer has multiple lines or not (Yes, No)
- **InternetService**: Customerβs internet service provider (DSL, Fiber optic, No)
- **OnlineSecurity**: Whether the customer has online security or not (Yes, No)
- **OnlineBackup**: Whether the customer has online backup or not (Yes, No)
- **DeviceProtection**: Whether the customer has device protection or not (Yes, No)
- **TechSupport**: Whether the customer has tech support or not (Yes, No)
- **StreamingTV**: Whether the customer has streaming TV or not (Yes, No)
- **StreamingMovies**: Whether the customer has streaming movies or not (Yes, No)
- **Contract**: The contract term of the customer (Month-to-month, One year, Two year)
- **PaperlessBilling**: Whether the customer has paperless billing or not (Yes, No)
- **PaymentMethod**: The customerβs payment method (Electronic check, Mailed check, Bank transfer (automatic), Credit card (automatic))
- **MonthlyCharges**: The amount charged to the customer monthly
- **TotalCharges**: The total amount charged to the customer
- **Churn** (the variable we want to predict): Whether the customer churned or not (Yes or No)
""", unsafe_allow_html=True)
st.markdown(" ")
st.markdown(" ")
## Exploratory data analysis
st.markdown("#### Exploratory Data Analysis π")
st.markdown("""Exploratory Data Analysis (EDA) is a crucial step in the machine learning workflow.
It helps practitioners understand the structure, patterns, and characteristics of the data they are working with.
For this use case, we will perform EDA by analyzing the **proportion of clients who have churned or not** based on the dataset's other variables.""")
st.info("**Note**: EDA is usually preformed before model training as it helps inform decisions made by the model throughout the modeling process.")
see_EDA = st.checkbox('**View the analysis**', key="churn-EDA")
if see_EDA:
st.markdown(" ")
# Show EDA image
st.markdown("""Exploratory Data Analysis has been preformed between the predicted variable `Churn` with 15 other variables present in the dataset.
Each graphs shows the proportion of churned and not churned customer based on the variable's possible values.""", unsafe_allow_html=True)
st.markdown(" ")
img_eda = os.path.join(path_churn, "EDA_churn.png")
st.image(img_eda)
st.markdown(" ")
# Intepretation
st.markdown("""**Interpretation**
For variables such as `Contract`, `PaperlessBilling`, `PaymentMethod` and `InternetService`, we can see a significant difference in the proportion of churned customers based on the variable's value.
In the *Contract* graph, clients with a 'Month-to-Month' tend to churn more often than those with a longer contract.
In the *InternetService* graph, clients with a 'Fiber optic' service are more likely to churn than those with DSL or no internet service. """, unsafe_allow_html=True)
st.info("""**Note**: Performing EDA can give us an indication as to which variables might be more significant in the customer churn model.
It can be a valuable tool to study the relationship between two variables but can sometimes be too simplistic. Some relationships might be top complex to be seen through EDA.""")
st.markdown(""" """)
st.markdown(""" """)
## Train the algorithm
st.markdown("#### Train the algorithm βοΈ")
st.markdown("""**Training the model** means feeding it data that contains multiple examples of what you are trying to predict (here it is `Churn`).
This allows the model to **learn relationships** between the `Churn` variable and the additional variables provided for the analysis and make accuracte predictions.""")
st.info("**Note**: A model is always trained before it can used to make predictions on new 'unlabeled' data.")
if 'model_train_churn' not in st.session_state:
st.session_state['model_train_churn'] = False
if st.session_state.model_train_churn:
st.write("The model has already been trained.")
else:
st.write("The model hasn't been trained yet")
run_churn_model = st.button("**Train the model**")
if run_churn_model:
st.session_state.model_train_churn = True
with st.spinner('Wait for it...'):
st.markdown(" ")
st.markdown(" ")
time.sleep(2)
st.markdown("#### See the results βοΈ")
tab1, tab2 = st.tabs(["Performance", "Explainability"])
######## MODEL PERFORMANCE
with tab1:
results_train = load_data_pickle(path_churn,"churn_cm_train.pkl")
results_train = results_train.to_numpy()
accuracy = np.round(results_train.diagonal()*100)
df_accuracy = pd.DataFrame({"Churn":["No","Yes"],
"Accuracy":accuracy})
df_accuracy["Accuracy"] = np.round(df_accuracy["Accuracy"]/100)
st.markdown(" ")
st.info("""**Note**: Evaluating a model's performance helps provide a quantitative measure of the model's ability to make accurate decisions.
In this use case, the performance of the customer churn model was measured by comparing the clients' churn variables with the value predicted by the trained model.""")
fig = px.bar(df_accuracy, y='Accuracy', x='Churn', color="Churn", title="Model performance", text_auto=True)
fig.update_traces(textfont_size=16)
#fig.update_traces(textposition='inside', textfont=dict(color='white'))
st.plotly_chart(fig, use_container_width=True)
# st.markdown("""The model's accuracy was measured for both Churn and No Churn.
# This is crucial as to understand whether the model is consistant in its performance, or whether it has trouble distinguishing between two kinds of credit score.""",
# unsafe_allow_html=True)
st.markdown(" ")
st.markdown("""**Interpretation**:
The model has a 88% accuracy in predicting customer that haven't churned, and a 94% accurate in predicting customer who have churned.
This means that the model's overall performance is good (at around 91%) but isn't equally as good for both predicted classes.
""", unsafe_allow_html=True)
##### MODEL EXPLAINABILITY
with tab2:
st.markdown(" ")
st.info("""**Note**: Explainability in AI refers to the ability to understand which variable used by a model during training had the most impact on the final predictions and how to quantify this impact.
Understanding the inner workings of a model helps build trust among users and stakeholders, as well as increase acceptance.""")
# Import feature importance dataframe
df_var_importance = load_data_pickle(path_churn, "churn_feature_importance.pkl")
df_var_importance.rename({"importance":"score"}, axis=1, inplace=True)
df_var_importance.sort_values(by=["score"], inplace=True)
df_var_importance["score"] = df_var_importance["score"].round(3)
# Feature importance plot with plotly
fig = px.bar(df_var_importance, x='score', y='variable', color="score", orientation="h", title="Model explainability")
st.plotly_chart(fig, use_container_width=True)
st.markdown("""Interpretation
The client's tenure, amount of Monthly and Total Charges, as well as the type of Contract had the most impact on the model's churn predictions.
On the other hand, whether the client is subscribed to a streaming platform, he is covered by device protection or he has or not phone service had a very contribution in the final predictions.
""", unsafe_allow_html=True)
st.markdown(" ")
st.markdown(" ")
st.markdown("#### Predict customer churn π")
st.markdown("Once you have trained the model, you can use it predict whether a client will churn or not on new data.")
st.markdown(" ")
col1, col2 = st.columns([0.25,0.75], gap="medium")
churn_test = load_data_pickle(path_churn,"churn_test_raw.pkl")
churn_test.reset_index(drop=True, inplace=True)
churn_test.insert(0, "Client ID", [f"{i}" for i in range(churn_test.shape[0])])
with col1:
st.markdown("""Filter the data
You can select clients based on their *Tenure*, *Total Charges* or *Contract*.""",
unsafe_allow_html=True)
select_image_box = st.radio(" ",
["Filter by Tenure", "Filter by Total Charges", "Filter by Contract", "No filters"],
label_visibility="collapsed")
if select_image_box == "Filter by Tenure":
st.markdown(" ")
min_tenure, max_tenure = st.slider('Select a range', churn_test["tenure"].astype(int).min(), churn_test["tenure"].astype(int).max(), (1,50),
key="tenure", label_visibility="collapsed")
churn_test = churn_test.loc[churn_test["tenure"].between(min_tenure,max_tenure)]
if select_image_box == "Filter by Total Charges":
st.markdown(" ")
min_charges, max_charges = st.slider('Select a range', churn_test["TotalCharges"].astype(int).min(), churn_test["TotalCharges"].astype(int).max(), (50, 5000),
label_visibility="collapsed", key="totalcharges")
churn_test = churn_test.loc[churn_test["TotalCharges"].between(min_charges, max_charges)]
if select_image_box == "Filter by Contract":
contract = st.selectbox('Select a type of contract', churn_test["Contract"].unique(), index=0, label_visibility="collapsed", key="contract",
placeholder = "Choose one or more options")
churn_test = churn_test.loc[churn_test["Contract"]==contract]
if select_image_box == "No filters":
pass
st.markdown(" ")
st.markdown("""Select a threshold for the alert
A warning message will be displayed if the percentage of churned customers exceeds this threshold.
""", unsafe_allow_html=True)
warning_threshold = st.slider('Select a value', min_value=20, max_value=100, step=10,
label_visibility="collapsed", key="warning")
st.markdown(" ")
st.write("The threshold is at", warning_threshold, "%")
with col2:
#st.markdown("**View the database**")
st.dataframe(churn_test)
# Button to make predictions
make_predictions = st.button("**Make predictions**")
st.markdown(" ")
if make_predictions:
if st.session_state.model_train_churn is True:
## Load preprocessed test data and model
churn_test_pp = load_data_pickle(path_churn, "churn_test_pp.pkl")
churn_model = load_model_pickle(path_pretrained_supervised,"churn_model.pkl")
X_test = churn_test_pp.iloc[churn_test.index,:].to_numpy()
predictions = churn_model.predict(X_test)
predictions = ["No" if x==0 else "Yes" for x in predictions]
df_results_pred = churn_test.copy()
df_results_pred["Churn"] = predictions
df_mean_pred = df_results_pred["Churn"].value_counts().to_frame().reset_index()
df_mean_pred.columns = ["Churn", "Proportion"]
df_mean_pred["Proportion"] = (100*df_mean_pred["Proportion"]/df_results_pred.shape[0]).round()
perct_churned = df_mean_pred.loc[df_mean_pred["Churn"]=="Yes"]["Proportion"].to_numpy()
if perct_churned >= warning_threshold:
st.error(f"The proportion of clients that have churned is above {warning_threshold}% (at {perct_churned[0]}%)β οΈ")
st.markdown(" ")
col1, col2 = st.columns([0.4,0.6], gap="large")
with col1:
st.markdown("**Proporition of predicted churn**")
fig = px.pie(df_mean_pred, values='Proportion', names='Churn', color="Churn",
color_discrete_map={'No':'royalblue', 'Yes':'red'})
st.plotly_chart(fig, use_container_width=True)
with col2:
df_show_results = df_results_pred[["Churn","Client ID"] + [col for col in df_results_pred.columns if col not in ["Client ID","Churn"]]]
columns_float = df_show_results.select_dtypes(include="float").columns
df_show_results[columns_float] = df_show_results[columns_float].astype(int)
def highlight_score(val):
if val == "No":
color = 'royalblue'
if val == 'Yes':
color= "red"
return f'color: {color}'
df_show_results_color = df_show_results.style.applymap(highlight_score, subset=['Churn'])
st.markdown("**Overall results**")
st.dataframe(df_show_results_color)
else:
st.error("You have to train the credit score model first.")
#######################################################################################################################
# UNSUPERVISED LEARNING
#######################################################################################################################
def markdown_general_info(df):
text = st.markdown(f"""
- **Age**: {int(np.round(df.Age))}
- **Yearly income**: {int(df.Income)} $
- **Number of kids**: {df.Kids}
- **Days of enrollment**: {int(np.round(df.Days_subscription))}
- **Web visits per month**: {df.WebVisitsMonth}
""")
return text
if learning_type == "Unsupervised Learning":
usl_usecase = st.selectbox("**Choose a use case**",
["Customer segmentation π§βπ€βπ§"])
#################################### CUSTOMER SEGMENTATION ##################################
path_clustering = r"data/clustering"
path_clustering_results = r"data/clustering/results"
if usl_usecase == "Customer segmentation π§βπ€βπ§":
# st.divider()
st.divider()
st.markdown("# Customer Segmentation π§βπ€βπ§")
st.info("""**Unsupervised learning** models are valulable tools for cases where you want your model to discover patterns by itself, without having to give it examples to learn from (especially if you don't have labeled data).
In this use case, we will show how they can be useful for **Customer Segmentation** to detect unknown groups of clients in a company's customer base.
Using this previously unknown segmentation, companies can then create more targeted add campaigns based on their consumer's behavior and preferences.
""")
st.markdown(" ")
## Show image
_, col, _ = st.columns([0.2,0.5,0.3])
with col:
st.image("images/cs.webp")
## About the use case
st.markdown("#### About the use case π")
st.markdown("""You are giving a database that contains information on around **2000 customers** of a mass-market retailer.
The database's contains **personal information** (age, income, number of kids...), as well as information on what types of products were purchased by the client, how long has he been enrolled as a client and where these purchases were made. """, unsafe_allow_html=True)
see_data = st.checkbox('**See the data**', key="dataframe")
if see_data:
customer_data = load_data_pickle(path_clustering, "clean_marketing.pkl")
st.dataframe(customer_data.head(10))
learn_data = st.checkbox('**Learn more about the variables**', key="variable")
if learn_data:
st.markdown("""
- **Age**: Customer's age
- **Income**: Customer's yearly household income
- **Kids**: Number of children/teenagers in customer's household
- **Days_subscription**: Number of days since a customer's enrollment with the company
- **Recency**: Number of days since customer's last purchase
- **Wines**: Proportion of money spent on wine in last 2 years
- **Fruits**: Proportion of money spent on fruits in last 2 years
- **MeatProducts**: Proportion of money spent on meat in last 2 years
- **FishProducts**: Proportion of money spent on fish in last 2 years
- **SweetProducts**: Proportion of money spent sweets in last 2 years
- **DealsPurchases**: Proportion of purchases made with a discount
- **WebPurchases**: Proportion of purchases made through the companyβs website
- **CatalogPurchases**: Proporition of purchases made using a catalogue
- **StorePurchases**: Proportion of purchases made directly in stores
- **WebVisitsMonth**: Proportion of visits to companyβs website in the last month""")
st.divider()
st.markdown(" ")
st.markdown(" ")
st.markdown("#### Clustering algorithm βοΈ")
st.info("""**Clustering** is a type of unsupervised learning method that learns how to group similar data points together into "clusters", without needing supervision.
In our case, a data points represents a customer that will be assigned to an unknown group.""")
st.markdown("""
- The clustering algorithm used in this use case allows a specific number of groups to be identified, which isn't the case for all clustering models.
- The number of clusters chosen by the user can have a strong impact on the quality of the segmentation. Try to run the model multiple times with different number of clusters and see which number leads to groups with more distinct customer behaviors/preferences.""")
st.markdown(" ")
st.markdown("Here is an example of grouped data using a clustering model.")
st.image("images/clustering.webp")
nb_groups = st.selectbox("Choose a number of customer groups to identify", np.arange(2,6))
df_results = load_data_pickle(path_clustering_results, f"results_{nb_groups}_clusters.pkl")
st.markdown(" ")
run_model = st.button("**Run the model**")
#tab1, tab2 = st.tabs(["Results per product type", "Results per channel"])
#st.divider()
if run_model:
cols_group = st.columns(int(nb_groups))
for nb in range(nb_groups):
df_nb = df_results[nb]
col1, col2 = st.columns([0.3,0.7])
with col1:
st.image("images/group.png", width=200)
st.header(f"Group {nb+1}", divider="grey")
markdown_general_info(df_nb)
with col2:
tab1, tab2 = st.tabs(["Results per product type", "Results per channel"])
list_product_col = [col for col in list(df_nb.index) if "Products" in col]
df_products = df_nb.reset_index()
df_products = df_products.loc[df_products["variable"].isin(list_product_col)]
df_products.columns = ["variables", "values"]
with tab1:
fig = px.pie(df_products, values='values', names='variables',
title="Amount spent per product type (in %)")
st.plotly_chart(fig, width=300)
list_purchases_col = [col for col in list(df_nb.index) if "Purchases" in col]
df_products = df_nb.reset_index()
df_products = df_products.loc[df_products["variable"].isin(list_purchases_col)]
df_products.columns = ["variables", "values"]
with tab2:
fig = px.pie(df_products, values='values', names='variables',
title='Proportion of purchases made per channel (in %)')
st.plotly_chart(fig, width=300)