File size: 9,457 Bytes
8118ad0
 
 
 
 
 
 
ee560c1
8118ad0
 
 
 
 
 
 
ed037db
8118ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6018fe5
8118ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cac1ecd
8118ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d80a44
 
 
8118ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6624b4b
4b40a38
8118ad0
 
 
 
 
 
 
6624b4b
4b40a38
5d80a44
 
 
8118ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d80a44
 
 
8118ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d4e906
8118ad0
 
 
 
 
 
 
 
 
 
 
 
 
ec08c27
540c0e7
8118ad0
bee7fe5
7d4e906
 
bee7fe5
 
8118ad0
 
 
 
 
 
 
 
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
# Importing required Library
import streamlit as st
import pandas as pd
import numpy as np
import os, pickle
from sklearn import preprocessing
from PIL import Image
from catboost import CatBoostClassifier


# Setting up page configuration and directory path
st.set_page_config(page_title="Customer churn prediction App", page_icon="🛳️", layout="centered")
DIRPATH = os.path.dirname(os.path.realpath(__file__))


# Setting background image 1
page_bg_img = '''
<style>
[data-testid="stAppViewContainer"] {
background-color:black;
background-image:
radial-gradient(white, rgba(255,255,255,.2) 2px, transparent 40px),
radial-gradient(white, rgba(255,255,255,.15) 1px, transparent 30px),
radial-gradient(white, rgba(255,255,255,.1) 2px, transparent 40px),
radial-gradient(rgba(255,255,255,.4), rgba(255,255,255,.1) 2px, transparent 30px);
background-size: 550px 550px, 350px 350px, 250px 250px, 150px 150px;
background-position: 0 0, 40px 60px, 130px 270px, 70px 100px;

}

</style>
'''
st.markdown(page_bg_img,unsafe_allow_html=True)



# Setting up logo
st.image('https://miro.medium.com/v2/resize:fit:786/format:webp/1*xT7y7u-DDssc3P_nT_qp2Q.png', width=700,caption=None, use_column_width=None, clamp=100, channels="RGB", output_format='JPEG')

 # Setting up Sidebar
social_acc = ['Data Field Description', 'EDA', 'About App']
social_acc_nav = st.sidebar.radio('**INFORMATION SECTION**', social_acc)

if social_acc_nav == 'Data Field Description':
     st.sidebar.markdown("""
    The table below gives a description on the variables required to make predictions.
    | Variable      | Definition:       |
    | :------------ |:--------------- |
    | FREQUENCE     | number of times the client has made an income |
    | TENURE        | duration in the network |
    | FREQUENCE_RECH| number of times the customer refilled |
    | MONTANT       | top-up amount   |
    | DATA_VOLUME   | number of connections|
    | ORANGE        | call to orange |
    | ARPU_SEGMENT  | income over 90 days / 3 |
    | ON_NET        | inter expresso call |
    | REGULARITY    | number of times the client is active for 90 days   |
    | FREQ_TOP_PACK | number of times client has activated the top pack packages|
    | REVENUE       | monthly income of each client   |
    """)

elif social_acc_nav == 'EDA':
    st.sidebar.markdown("<h2 style='text-align: center;'> Exploratory Data Analysis </h2> ", unsafe_allow_html=True)
    st.sidebar.markdown('''---''')
    st.sidebar.markdown('''The exploratory data analysis of this project can be find in a Jupyter notebook from the linl below''')
    st.sidebar.markdown("[Open Notebook](https://github.com/Gyimah3/Expresso_Customer_Churn_Prediction/blob/main/Capstone_Project_Customer_Churn_3_L.ipynb)")

elif social_acc_nav == 'About App':
    st.sidebar.markdown("<h2 style='text-align: center;'> Customer Churn prediction App </h2> ", unsafe_allow_html=True)
    st.sidebar.markdown('''---''')
    st.sidebar.markdown("""
                        | Brief Introduction|
                        | :------------ |
                        This projet is based on a Zindi challenge for an African telecommunications company (Expresso)
                        that provides customers with airtime and mobile data bundles. The objective of this challenge
                        is to develop a machine learning model to predict the likelihood of each customer “churning,”
                        i.e. becoming inactive and not making any transactions for 90 days. This solution will help
                        this telecom company to better serve their customers by understanding which customers are at risk of leaving""")
    st.sidebar.markdown("")
    st.sidebar.markdown("[ Visit Github Repository for more information](https://github.com/Gyimah3/Store-Sales----Time-Series-Forecasting-Regression-project-)")
    st.sidebar.markdown("Dedicated to: mom❄️ and Sis Evelyn❄️.")
    st.sidebar.markdown("")
    
# Config & Setup
@st.cache(allow_output_mutation=True)
def Load_ml_items(relative_path):
    "Load ML items to reuse them"
    with open(relative_path, 'rb') as file:
        loaded_object = pickle.load(file)
    return loaded_object


loaded_object = Load_ml_items(r'ml_comps_1.pkl')



pipeline_of_my_model = loaded_object["pipeline"]
num_cols = loaded_object['numeric_columns']
cat_cols = loaded_object['categorical_columns']



# Setting up variables for input data
@st.cache()
def setup(tmp_df_file):
    "Setup the required elements like files, models, global variables, etc"
    pd.DataFrame(
        dict(
            TENURE=[],
            MONTANT=[],
            FREQUENCE_RECH=[],
            REVENUE=[],
            ARPU_SEGMENT=[],
            FREQUENCE=[],
            DATA_VOLUME=[],
            ON_NET=[],
            ORANGE=[],
            #TIGO=[],
            #ZONE1=[],
            #ZONE2=[],
            REGULARITY=[],
            FREQ_TOP_PACK=[]
           
        )
    ).to_csv(tmp_df_file, index=False)

# Setting up a file to save our input data
tmp_df_file = os.path.join(DIRPATH, "tmp", "data.csv")
setup(tmp_df_file)

# setting Title for forms
st.markdown("<h2 style='text-align: center;'> Customer Churn Prediction </h2> ", unsafe_allow_html=True)
st.markdown("<h7 style='text-align: center;'> Fill in the details below and click on SUBMIT button to make a prediction </h7> ", unsafe_allow_html=True)


# Creating columns for for input data(forms)
left_col,right_col = st.columns(2)#[#20,20],gap="small")

# Developing forms to collect input data
with st.form(key="information", clear_on_submit=True):
    # Setting up input data for 1st column
    left_col.markdown("**FIRST COLUMN DATA**")
    TENURE= left_col.selectbox("duration in the network:",['K > 24 month','J 21-24 month','I 18-21 month','H 15-18 month','G 12-15 month','F 9-12 month',  'E 6-9 month', 'D 3-6 month'])
    MONTANT = left_col.number_input("top-up amount:",  min_value=0, max_value= 1000000000)
    FREQUENCE_RECH = left_col.number_input("number of times the customer refilled:", min_value=0, max_value=1000)
    REVENUE = left_col.number_input("monthly income of each client:", min_value=0, max_value=1000000000)
    ARPU_SEGMENT=left_col.number_input("income over 90 days / 3:",min_value=0, max_value=30000)
    FREQUENCE = left_col.number_input("number of times the client has made an income:", min_value=0, max_value=100)
    #DATA_VOLUME = left_col.number_input("number of connections:",min_value=0, max_value=10000)
    

    
     # Setting up input data for 2nd column
    right_col.markdown("**SECOND COLUMN DATA**")
    ON_NET = right_col.number_input("inter expresso call:", min_value=0, max_value=10000)
    ORANGE = right_col.number_input("call to orange:", min_value=0, max_value=10000)
    #FREQUENCE = left_col.number_input("number of times the client has made an income:", min_value=0, max_value=100)
    DATA_VOLUME = left_col.number_input("number of connections:",min_value=0, max_value=10000)
    #TIGO = right_col.number_input("call to Tigo:", min_value=0, max_value=10000)
    #ZONE1 = right_col.number_input("call to Zones 1:", min_value=0, max_value=10000)
    #ZONE2 = right_col.number_input("call to Zones 2:", min_value=0, max_value=10000)
    REGULARITY = right_col.number_input("number of times the client is active for 90 day:", min_value=0, max_value=100)
    FREQ_TOP_PACK=right_col.number_input("number of times the client has activated the top pack packages:", min_value=0, max_value=100)

    submitted = st.form_submit_button(label="Submit")
    
if submitted:
    # Saving input data as csv after submission
    pd.read_csv(tmp_df_file).append(
        dict(
            
               
                TENURE=TENURE,
                MONTANT=MONTANT,
                FREQUENCE_RECH=FREQUENCE_RECH,
                REVENUE=REVENUE,
                ARPU_SEGMENT=ARPU_SEGMENT,
                FREQUENCE=FREQUENCE,
                DATA_VOLUME=DATA_VOLUME,
                ON_NET=ON_NET,
                ORANGE=ORANGE,
                #TIGO=TIGO,
                #ZONE1=ZONE1,
                #ZONE2=ZONE2,
                REGULARITY=REGULARITY,
                FREQ_TOP_PACK=FREQ_TOP_PACK
           
            ),
            ignore_index=True,
    ).to_csv(tmp_df_file, index=False)
    st.balloons()
     

    df = pd.read_csv(tmp_df_file)
    df= df.copy()
   
        
    # Making Predictions
    # Passing data to pipeline to make prediction
    pred_output = pipeline_of_my_model.predict(df)
    prediction_proba = pipeline_of_my_model.predict_proba(df)
    
    # Interpleting prediction output for display
    X= pred_output[-1]
    if X == 1:
        explanation = 'Person will Churn'
    else: 
        explanation = "Person won't churn"
    output = explanation
    

    # Displaying prediction results
    st.markdown('''---''')
    st.markdown("<h4 style='text-align: center;'> Prediction Results </h4> ", unsafe_allow_html=True)
    st.success(f"Prediction: {output}")
    st.markdown('''----''')

    st.subheader('Prediction Percentages:') 
    st.write('**Probablity of Person not churning is ( in % )**:',prediction_proba[0][0]*100)
    st.write('**Probablity of Person not churning is ( in % )**:',prediction_proba[0][1]*100)
    

    # Making expander to view all records
    expander = st.expander("See all records")
    with expander:
        df = pd.read_csv(tmp_df_file)
        df['CHURN']= pred_output
        st.dataframe(df)