File size: 45,846 Bytes
c2522bb
 
 
 
 
 
75a7004
c2522bb
b2174f1
aa667a1
c2522bb
 
 
 
 
aa667a1
c548d40
c2522bb
 
 
 
 
 
 
b2174f1
 
c2522bb
b2174f1
 
 
c2522bb
b2174f1
 
c2522bb
b2174f1
c2522bb
b2174f1
 
 
 
ce45b2a
c2522bb
b2174f1
 
 
c2522bb
b2174f1
 
 
 
c2522bb
 
b2174f1
 
 
 
c2522bb
b2174f1
c2522bb
b2174f1
 
 
c2522bb
 
 
 
 
b2174f1
 
 
c2522bb
 
b2174f1
 
 
 
ce45b2a
c2522bb
 
b2174f1
 
 
 
 
 
 
 
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
c2522bb
b2174f1
 
 
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
b2174f1
 
 
 
 
c2522bb
b2174f1
 
 
c2522bb
b2174f1
 
c2522bb
b2174f1
 
 
 
c2522bb
b2174f1
c2522bb
b2174f1
 
 
c2522bb
 
b2174f1
 
 
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
b2174f1
 
 
c2522bb
b2174f1
 
 
 
 
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
b2174f1
 
 
c2522bb
b2174f1
 
 
 
 
c2522bb
b2174f1
 
 
 
 
c2522bb
b2174f1
 
 
 
c2522bb
b2174f1
 
c2522bb
b2174f1
 
 
 
 
 
c2522bb
b2174f1
 
c2522bb
 
b2174f1
 
 
 
 
 
c2522bb
b2174f1
 
 
 
c2522bb
b2174f1
 
 
 
 
c2522bb
b2174f1
c2522bb
b2174f1
 
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
b2174f1
 
75a7004
 
 
 
 
 
b2174f1
75a7004
b2174f1
 
 
75a7004
b2174f1
 
 
75a7004
b2174f1
 
 
75a7004
b2174f1
75a7004
 
 
b2174f1
 
75a7004
b2174f1
 
 
75a7004
b2174f1
75a7004
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75a7004
b2174f1
 
75a7004
 
b2174f1
 
 
 
 
 
 
75a7004
b2174f1
 
 
75a7004
b2174f1
 
 
75a7004
b2174f1
 
 
 
75a7004
 
b2174f1
 
 
 
 
 
 
 
75a7004
b2174f1
 
 
 
 
 
 
 
 
 
75a7004
 
b2174f1
 
75a7004
b2174f1
 
 
 
75a7004
b2174f1
75a7004
b2174f1
 
 
 
75a7004
b2174f1
 
 
 
75a7004
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
75a7004
b2174f1
 
 
 
75a7004
b2174f1
 
 
75a7004
b2174f1
75a7004
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75a7004
b2174f1
 
75a7004
b2174f1
75a7004
b2174f1
 
 
 
 
75a7004
b2174f1
 
 
 
75a7004
b2174f1
 
 
75a7004
b2174f1
 
 
 
 
75a7004
b2174f1
 
 
 
 
75a7004
b2174f1
 
 
 
75a7004
b2174f1
 
75a7004
b2174f1
 
 
 
 
 
75a7004
b2174f1
 
75a7004
 
b2174f1
 
 
75a7004
 
b2174f1
 
 
75a7004
b2174f1
 
75a7004
b2174f1
 
 
75a7004
b2174f1
 
 
 
 
 
 
 
 
75a7004
b2174f1
c2522bb
b2174f1
 
c2522bb
b2174f1
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
b2174f1
 
c2522bb
 
 
 
 
 
b2174f1
 
 
c2522bb
 
b2174f1
 
 
 
 
 
 
 
 
c2522bb
 
 
b2174f1
 
 
ce45b2a
b2174f1
 
 
 
 
 
 
 
 
c2522bb
b2174f1
c2522bb
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
 
b2174f1
 
c2522bb
b2174f1
ce45b2a
b2174f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2522bb
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
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, check_password
from st_pages import add_indentation

#####################################################################################
#                                    PAGE CONFIG
#####################################################################################

#add_indentation()
st.set_page_config(layout="wide")



#####################################################################################
#                                    INTRO 
#####################################################################################

if check_password():
    st.markdown("# Supervised vs Unsupervised Learning 🔍")

    st.info("""Data Science models are often split into two categories: **Supervised** and **Unsupervised Learning**. 
            The goal of this page is to present these two kinds of Data Science models, as well as give you multiple use cases to try them with.
            Note that other kinds of AI models exist such as Reinforcement Learning or Federated Learning, which we won't cover in this app.""")

    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**. <br>
                    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, which doesn't contain the desired output.
- 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 an AI task**", 
                                ["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 models** are supervised learning models whose goal is to categorize data into predefined categories. 
                    As opposed to unsupervised learning models, these categories are known beforehand.
                    Other types of supervised learning models include Regression models, which learn how to predict numerical values, instead of a set number of categories.""")
            
            st.markdown("In this use case, we will build a **credit score classification model** which predicts whether a client has 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 7600 clients and containing bank and credit-related client information. <br> 
                        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("""<i>The model's accuracy was measured for every type of credit score (Good, Standard, Poor).</i>
                                    <i>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.</i>""", 
                                    unsafe_allow_html=True)

                        st.markdown(" ")

                        st.markdown("""**Interpretation**: <br>
                                    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("""<b>Interpretation</b>: <br>
                                    A client's outstanding debt, interest rate and delay from due date were the most crucial factors in explaining their final credit score. <br>
                                    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("""<b>Filter the data</b> <br>
                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("""<b>Select a threshold for the alert</b> <br>
                        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 models** are supervised learning models whose goal is to categorize data into predefined categories. 
                    As opposed to unsupervised learning models, these categories are known beforehand.
                    Other types of supervised learning models include Regression models, which learn how to predict numerical values, instead of a set number of categories.""")

            st.markdown("For this use case, we will build a **customer churn classification model** that can predict whether a person will stop being a customer 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 model, you were provided a **labeled** database with around 7000 clients of a telecommunications company. <br>
                        The data contains information on which services the customer has signed for, account information 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
    - <span style="color: red;"> **Churn** (the variable we want to predict): Whether the customer churned or not (Yes or No) </span>
    """, 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. <br>
                            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** <br>
                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("""<i>The model's accuracy was measured for both Churn and No Churn.</i>
                        #             <i>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.</i>""", 
                        #             unsafe_allow_html=True)

                        st.markdown(" ")

                        st.markdown("""**Interpretation**: <br>
                                    The model has a 88% accuracy in predicting customer that haven't churned, and a 94% accurate in predicting customer who have churned. <br>
                                    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("""<b>Interpretation</b> <br>
                                    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("""<b>Filter the data</b> <br>
                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("""<b>Select a threshold for the alert</b> <br>
                        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 (clustering) 🧑‍🤝‍🧑"])
        

        #################################### CUSTOMER SEGMENTATION ##################################

        path_clustering = r"data/clustering"
        path_clustering_results = r"data/clustering/results"

        if usl_usecase == "Customer segmentation (clustering) 🧑‍🤝‍🧑":

            # st.divider()
            st.divider()
            st.markdown("# Customer Segmentation (clustering) 🧑‍🤝‍🧑")

            st.markdown("""In this use case, we will use a clustering model, a type of Unsupervised Learning model, to perform **Customer Segmentation**. <br>
                        Our model will allow similar groups of clients to be identified within company's consumer database based on consumer habits and caracteristics. 
            """, unsafe_allow_html=True)
            
            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.""")

            st.markdown(" ")
            st.markdown("Here is an example of grouped data using a clustering model.")
            st.image("images/clustering.webp")

            st.warning("**Note**: 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.")                    

            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)