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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Importing required Libraries
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os, pickle
|
| 6 |
+
|
| 7 |
+
# Setting up page configuration and directory path
|
| 8 |
+
st.set_page_config(page_title= "Customer Churn Prediction", page_icon="🛳️", layout="centered")
|
| 9 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
| 10 |
+
|
| 11 |
+
# Setting background image
|
| 12 |
+
import base64
|
| 13 |
+
def add_bg_from_local(image_file):
|
| 14 |
+
with open(image_file, "rb") as image_file:
|
| 15 |
+
encoded_string = base64.b64encode(image_file.read())
|
| 16 |
+
st.markdown(
|
| 17 |
+
f"""
|
| 18 |
+
<style>
|
| 19 |
+
.stApp {{
|
| 20 |
+
background-image: url(data:image/{"jpg"};base64,{encoded_string.decode()});
|
| 21 |
+
background-size: cover
|
| 22 |
+
}}
|
| 23 |
+
</style>
|
| 24 |
+
""",
|
| 25 |
+
unsafe_allow_html=True
|
| 26 |
+
)
|
| 27 |
+
add_bg_from_local('images/background.jpg')
|
| 28 |
+
|
| 29 |
+
# Setting up logo
|
| 30 |
+
left, mid, mid1, right = st.columns(4)
|
| 31 |
+
with mid:
|
| 32 |
+
st.image("images/logo.jpeg", use_column_width=True)
|
| 33 |
+
|
| 34 |
+
# Setting up Sidebar
|
| 35 |
+
social_acc = ['Data Field Description', 'EDA', 'About App']
|
| 36 |
+
social_acc_nav = st.sidebar.radio('**INFORMATION SECTION**', social_acc)
|
| 37 |
+
|
| 38 |
+
if social_acc_nav == 'Data Field Description':
|
| 39 |
+
st.sidebar.markdown("<h2 style=[],'text-align: center;'> Data Field Description </h2> ", unsafe_allow_html=True)
|
| 40 |
+
st.sidebar.markdown("""
|
| 41 |
+
The table below gives a description on the variables required to make predictions.
|
| 42 |
+
| Variable | Definition: |
|
| 43 |
+
| :------------ |:--------------- |
|
| 44 |
+
| FREQUENCE | number of times the client has made an income |
|
| 45 |
+
| TENURE | duration in the network |
|
| 46 |
+
| FREQUENCE_RECH| number of times the customer refilled |
|
| 47 |
+
| MONTANT | top-up amount |
|
| 48 |
+
| DATA_VOLUME | number of connections|
|
| 49 |
+
| ORANGE | call to orange |
|
| 50 |
+
| ARPU_SEGMENT | income over 90 days / 3 |
|
| 51 |
+
| ON_NET | inter expresso call |
|
| 52 |
+
| REGULARITY | number of times the client is active for 90 days |
|
| 53 |
+
| FREQ_TOP_PACK | number of times client has activated the top pack packages|
|
| 54 |
+
| REVENUE | monthly income of each client |
|
| 55 |
+
""")
|
| 56 |
+
|
| 57 |
+
elif social_acc_nav == 'EDA':
|
| 58 |
+
st.sidebar.markdown("<h2 style=[],'text-align: center;'> Exploratory Data Analysis </h2> ", unsafe_allow_html=True)
|
| 59 |
+
st.sidebar.markdown('''---''')
|
| 60 |
+
st.sidebar.markdown("""
|
| 61 |
+
| About EDA|
|
| 62 |
+
| :------------ |
|
| 63 |
+
The exploratory data analysis of this project can be found in a Notebook in a github repository from the link below""" )
|
| 64 |
+
st.sidebar.markdown("[Open Repository ](https://github.com/Kyei-frank/Customer-Churn-Prediction--Expresso)")
|
| 65 |
+
|
| 66 |
+
elif social_acc_nav == 'About App':
|
| 67 |
+
st.sidebar.markdown("<h2 style=[],'text-align: center;'> Titanic Survival Prediction App </h2> ", unsafe_allow_html=True)
|
| 68 |
+
st.sidebar.markdown('''---''')
|
| 69 |
+
st.sidebar.markdown("""
|
| 70 |
+
| Brief Introduction|
|
| 71 |
+
| :------------ |
|
| 72 |
+
This projet is based on a Zindi challenge for an African telecommunications company (Expresso)
|
| 73 |
+
that provides customers with airtime and mobile data bundles. The objective of this challenge
|
| 74 |
+
is to develop a machine learning model to predict the likelihood of each customer “churning,”
|
| 75 |
+
i.e. becoming inactive and not making any transactions for 90 days. This solution will help
|
| 76 |
+
this telecom company to better serve their customers by understanding which customers are at risk of leaving""")
|
| 77 |
+
st.sidebar.markdown("")
|
| 78 |
+
st.sidebar.markdown("[ Visit Github Repository for more information](https://github.com/Kyei-frank/Titanic-Project---Machine-Learning-from-Disaster)")
|
| 79 |
+
|
| 80 |
+
# Loading Machine Learning Objects
|
| 81 |
+
@st.cache_data
|
| 82 |
+
def load_saved_objects(file_path = 'ML_items'):
|
| 83 |
+
# Function to load saved objects
|
| 84 |
+
with open('ML_items', 'rb') as file:
|
| 85 |
+
loaded_object = pickle.load(file)
|
| 86 |
+
|
| 87 |
+
return loaded_object
|
| 88 |
+
|
| 89 |
+
# Instantiating ML_items
|
| 90 |
+
Loaded_object = load_saved_objects(file_path = 'ML_items')
|
| 91 |
+
pipeline_of_my_app = Loaded_object["pipeline"]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# Setting up variables for input data
|
| 95 |
+
@st.cache_data
|
| 96 |
+
def setup(tmp_df_file):
|
| 97 |
+
"Setup the required elements like files, models, global variables, etc"
|
| 98 |
+
pd.DataFrame(
|
| 99 |
+
dict(
|
| 100 |
+
FREQUENCE= [],
|
| 101 |
+
TENURE= [],
|
| 102 |
+
FREQUENCE_RECH= [],
|
| 103 |
+
MONTANT= [],
|
| 104 |
+
DATA_VOLUME= [],
|
| 105 |
+
ORANGE= [],
|
| 106 |
+
ARPU_SEGMENT= [],
|
| 107 |
+
ON_NET= [],
|
| 108 |
+
REGULARITY= [],
|
| 109 |
+
FREQ_TOP_PACK= [],
|
| 110 |
+
REVENUE= [],
|
| 111 |
+
)
|
| 112 |
+
).to_csv(tmp_df_file, index=False)
|
| 113 |
+
|
| 114 |
+
# Setting up a file to save our input data
|
| 115 |
+
tmp_df_file = os.path.join(DIRPATH, "tmp", "data.csv")
|
| 116 |
+
setup(tmp_df_file)
|
| 117 |
+
|
| 118 |
+
# setting Title for forms
|
| 119 |
+
st.markdown("<h2 style=[],'text-align: right;'>....... Customer Churn Prediction ...... </h2> ", unsafe_allow_html=True)
|
| 120 |
+
st.markdown("<h6 style=[],'text-align: center;'> Fill in the details below and click on SUBMIT button to make a prediction for a Client. </h6> ", unsafe_allow_html=True)
|
| 121 |
+
|
| 122 |
+
# Creating columns for input data(forms)
|
| 123 |
+
left_col, middle_col, right_col = st.columns(3)
|
| 124 |
+
|
| 125 |
+
# Developing forms to collect input data
|
| 126 |
+
with st.form(key="information", clear_on_submit=True):
|
| 127 |
+
|
| 128 |
+
# Setting up input data for 1st column
|
| 129 |
+
left_col.markdown(":blue[**CALLS & ACTIVITY DETAILS**]")
|
| 130 |
+
ORANGE = left_col.number_input("Insert Number of calls to ORANGE")
|
| 131 |
+
ON_NET = left_col.number_input("Insert Number of inter expresso calls")
|
| 132 |
+
DATA_VOLUME = left_col.number_input("Insert Number of connections")
|
| 133 |
+
REGULARITY = left_col.number_input("Insert number of times the client is active for 90 days")
|
| 134 |
+
FREQ_TOP_PACK = left_col.number_input("Insert number of times client has activated the top pack packages")
|
| 135 |
+
|
| 136 |
+
# Setting up input data for 2nd column
|
| 137 |
+
middle_col.markdown(":blue[**TOP-UP & INCOME DETAILS**]")
|
| 138 |
+
MONTANT = middle_col.number_input("Insert top-up amount")
|
| 139 |
+
FREQUENCE_RECH = middle_col.number_input("Insert Number of times the customer refilled")
|
| 140 |
+
REVENUE = middle_col.number_input("Insert monthly income of client")
|
| 141 |
+
ARPU_SEGMENT = middle_col.number_input("Insert income over 90 days / 3")
|
| 142 |
+
FREQUENCE = middle_col.number_input("Insert number of times client has made an income")
|
| 143 |
+
|
| 144 |
+
# Setting up input data for 2nd column
|
| 145 |
+
right_col.markdown(":blue[**TENURE DETAILS**]")
|
| 146 |
+
TENURE = right_col.radio("What is Client's duration in the network?", ('D 3-6 month',
|
| 147 |
+
'E 6-9 month', 'F 9-12 month', 'G 12-15 month', 'H 15-18 month',
|
| 148 |
+
'I 18-21 month', 'J 21-24 month', 'K > 24 month',))
|
| 149 |
+
|
| 150 |
+
submitted = st.form_submit_button(label="Submit")
|
| 151 |
+
|
| 152 |
+
# Setting up background operations after submitting forms
|
| 153 |
+
if submitted:
|
| 154 |
+
# Saving input data as csv after submission
|
| 155 |
+
pd.read_csv(tmp_df_file).append(
|
| 156 |
+
dict(
|
| 157 |
+
FREQUENCE= FREQUENCE,
|
| 158 |
+
TENURE= TENURE,
|
| 159 |
+
FREQUENCE_RECH= FREQUENCE_RECH,
|
| 160 |
+
MONTANT= MONTANT,
|
| 161 |
+
DATA_VOLUME= DATA_VOLUME,
|
| 162 |
+
ORANGE= ORANGE,
|
| 163 |
+
ARPU_SEGMENT= ARPU_SEGMENT,
|
| 164 |
+
ON_NET= ON_NET,
|
| 165 |
+
REGULARITY= REGULARITY,
|
| 166 |
+
FREQ_TOP_PACK= FREQ_TOP_PACK,
|
| 167 |
+
REVENUE= REVENUE,
|
| 168 |
+
),
|
| 169 |
+
ignore_index=True,
|
| 170 |
+
).to_csv(tmp_df_file, index=False)
|
| 171 |
+
st.balloons()
|
| 172 |
+
|
| 173 |
+
# Converting input data to a dataframe for prediction
|
| 174 |
+
df = pd.read_csv(tmp_df_file)
|
| 175 |
+
df= df.copy()
|
| 176 |
+
|
| 177 |
+
# Making Predictions
|
| 178 |
+
# Passing data to pipeline to make prediction
|
| 179 |
+
pred_output = pipeline_of_my_app.predict(df)
|
| 180 |
+
prob_output = np.max(pipeline_of_my_app.predict_proba(df))
|
| 181 |
+
|
| 182 |
+
# Interpleting prediction output for display
|
| 183 |
+
X= pred_output[-1]
|
| 184 |
+
if X == 1:
|
| 185 |
+
explanation = 'Client will CHURN'
|
| 186 |
+
else:
|
| 187 |
+
explanation = 'Client will NOT CHURN'
|
| 188 |
+
output = explanation
|
| 189 |
+
|
| 190 |
+
# Displaying prediction results
|
| 191 |
+
st.markdown('''---''')
|
| 192 |
+
st.markdown("<h4 style=[],'text-align: center;'> Prediction Results </h4> ", unsafe_allow_html=True)
|
| 193 |
+
st.success(f"Prediction: {output}")
|
| 194 |
+
st.success(f"Confidence Probability: {prob_output}")
|
| 195 |
+
st.markdown('''---''')
|
| 196 |
+
|
| 197 |
+
# Making expander to view all records
|
| 198 |
+
expander = st.expander("See all records")
|
| 199 |
+
with expander:
|
| 200 |
+
df = pd.read_csv(tmp_df_file)
|
| 201 |
+
df['Survival']= pred_output
|
| 202 |
+
st.dataframe(df)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas==1.4.2
|
| 3 |
+
numpy==1.21.5
|
| 4 |
+
seaborn==0.11.2
|
| 5 |
+
scikit-learn
|