dhikri commited on
Commit
8a6973e
1 Parent(s): f989256

Upload 7 files

Browse files
Files changed (7) hide show
  1. app.py +20 -0
  2. churn_best_model.h5 +3 -0
  3. eda.py +100 -0
  4. image_pred.jpg +0 -0
  5. prediction.py +86 -0
  6. preprocessor_churn.pkl +3 -0
  7. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import eda
3
+ import prediction
4
+ from PIL import Image
5
+
6
+ # Set Config dan icon
7
+ st.set_page_config(
8
+ page_title='Churn Prediction',
9
+ layout='wide',
10
+ )
11
+
12
+ #klik eda, bawa ke page eda
13
+ navigation = st.sidebar.selectbox('Select Page (EDA/Churn Prediction):', ('EDA', 'Churn Prediction'))
14
+
15
+
16
+ #Run modul dengan if else
17
+ if navigation == 'EDA':
18
+ eda.run()
19
+ else :
20
+ prediction.run()
churn_best_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a1b8f9f4329696af5ceb64050282eca7f556586ef77213e85cfb571c80e8de09
3
+ size 262152
eda.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import seaborn as sns
4
+ import matplotlib.pyplot as plt
5
+ import plotly.express as px
6
+ from PIL import Image
7
+
8
+ def run() :
9
+ # Membuat Sub Header
10
+ st.title('**EDA Churn Customer**')
11
+ st.write('~ P2M1-Desti Ratna Komala-RMT-020')
12
+ #menambahkan Gambar
13
+ image = Image.open('image_pred.jpg')
14
+ st.image(image, caption='churn.jpg')
15
+
16
+ # Import df
17
+ df_eda = pd.read_csv('https://raw.githubusercontent.com/destiratnakomala/Hacktiv8_Folder/main/churn.csv')
18
+
19
+ #membuat garis lurus
20
+ st.markdown('-----')
21
+
22
+ #magic syntax
23
+ '''
24
+ Pada page ini, akan dilakukan ekplorasi dataset sederhana.
25
+ Berikut adalah dataset Customer Churn yang digunakan
26
+ '''
27
+ #membuat title
28
+ st.subheader('Customer Churn EDA')
29
+
30
+ # Import df
31
+ df = pd.read_csv('https://raw.githubusercontent.com/destiratnakomala/Hacktiv8_Folder/main/churn.csv')
32
+ st.dataframe(df)
33
+
34
+
35
+
36
+
37
+
38
+
39
+ st.write('Dari visualisasi dibawah dapat disimpulkan bahwa :')
40
+ st.markdown('- *Customer* yang *churn* lebih banyak dari pada *customer* yang tidak *churn*')
41
+ #membuat garis lurus
42
+ st.markdown('-----')
43
+ pilihanuser=st.selectbox('Pilih Feature:', ('gender', 'region_category', 'membership_category', 'joined_through_referral', 'preferred_offer_types', 'medium_of_operation', 'internet_option', 'used_special_discount', 'offer_application_preference', 'past_complaint', 'complaint_status', 'feedback'))
44
+
45
+
46
+ #--------------------------------------------
47
+
48
+ fig, ax =plt.subplots(1,3,figsize=(25,8))
49
+ fig.suptitle(f'{pilihanuser} terhadap Customer Churn', fontsize=17, fontweight='bold')
50
+ #visualisasikan data
51
+
52
+ plt.subplot(1,3,1)
53
+ plt.title(f'{pilihanuser} terhadap Customer Churn', fontsize=14)
54
+ ax = sns.countplot(data = df, x = df[pilihanuser], hue="churn_risk_score", palette = 'pastel')
55
+ plt.ylabel("#churn", fontsize= 14)
56
+ plt.xticks(rotation=90)
57
+ plt.legend(loc=0)
58
+
59
+
60
+ plt.subplot(1,3,2)
61
+ plt.title(f'{pilihanuser} terhadap %Customer Churn', fontsize=12)
62
+ ax = sns.barplot(x = df[pilihanuser], y = "churn_risk_score", data = df, palette = sns.color_palette('pastel')[1:2], errorbar= None)
63
+ plt.ylabel("%Churn", fontsize= 14)
64
+ plt.xticks(rotation=90)
65
+
66
+ for p in ax.patches:
67
+ ax.annotate("%.2f" %(p.get_height()), (p.get_x()+0.3, p.get_height()+0.005),fontsize=12)
68
+
69
+ plt.subplot(1,3,3)
70
+ round(df[pilihanuser].value_counts()/df.shape[0]*500,2).plot.pie(autopct= '%1.1f%%',colors=sns.color_palette('pastel')[0:10],shadow=True, legend=None)
71
+ plt.ylabel("")
72
+ plt.xticks(rotation=45)
73
+ plt.title(pilihanuser, fontsize=14)
74
+ st.pyplot(fig)
75
+
76
+
77
+ st.markdown('-----')
78
+ fig=plt.figure(figsize=(15, 5))
79
+ pilihanuser2=st.selectbox('Pilih Feature Numerik:',('age','days_since_last_login', 'avg_time_spent','avg_transaction_value', 'avg_frequency_login_days', 'points_in_wallet'))
80
+ plt.subplot(1,2,1)
81
+ sns.distplot(df[pilihanuser2])
82
+ plt.ticklabel_format(style='plain', axis='x')
83
+ plt.ylabel('')
84
+
85
+ plt.subplot(1,2,2)
86
+ sns.kdeplot(df.loc[(df['churn_risk_score'] == 0), pilihanuser2], label = 'not_churn', fill = True)
87
+ sns.kdeplot(df.loc[(df['churn_risk_score'] == 1), pilihanuser2], label = 'churn', fill = True)
88
+ plt.ylabel('')
89
+ plt.legend()
90
+ st.pyplot(fig)
91
+
92
+
93
+
94
+
95
+ #membuat garis lurus
96
+ st.markdown('-----')
97
+
98
+
99
+ if __name__ == '__main__':
100
+ run()
image_pred.jpg ADDED
prediction.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import pickle
5
+ from tensorflow.keras.models import load_model
6
+
7
+
8
+ def run() :
9
+ # Load Model
10
+ with open('preprocessor_churn.pkl', 'rb') as file_1:
11
+ preprocessor = pickle.load(file_1)
12
+ model_churn = load_model('churn_best_model.h5', compile=False)
13
+
14
+ # Membuat Title
15
+ st.markdown("<h1 style='text-align: center;'>Churn Customer Prediction</h1>", unsafe_allow_html=True)
16
+
17
+ # Menambahkan Deskripsi form
18
+ st.write('Page ini berisi pemodelan untuk memprediksi churn customer. Silakan masukkan data Anda pada form dibawah ini.')
19
+
20
+ #Membuat Form
21
+ with st.form(key= 'form_customer'):
22
+
23
+ st.markdown('### **Data Customer**')
24
+ user_id = st.text_input('User ID',value= '')
25
+ gender = st.selectbox('Gender',('M','F'),index=1)
26
+ age = st.slider('Age',10,90,30)
27
+ region_category = st.radio('Region', options=['City','Village','Town'], horizontal=True)
28
+ internet_option = st.selectbox('Internet Option',('Wi-Fi','Fiber_Optic', 'Mobile_Data'),index=1)
29
+ medium_of_operation = st.radio('Medium', options=['Desktop','Smartphone','Both'], horizontal=True)
30
+ st.markdown('---')
31
+ st.markdown('### **Login Data**')
32
+ days_since_last_login = st.slider('Days Since Last Login',0,30,3)
33
+ avg_frequency_login_days = st.slider('Avg Frequency Login Days',0,90,14)
34
+ st.markdown('---')
35
+ st.markdown('### **Membership Data**')
36
+ joined_through_referral = st.selectbox('Referral',('Yes','No'),index=1)
37
+ membership_category = st.selectbox('Membership Category',('No Membership','Basic Membership','Silver Membership', 'Premium Membership', 'Gold Membership', 'Platinum Membership'),index=1)
38
+ st.markdown('---')
39
+ st.markdown('### **Transaction Data**')
40
+ points_in_wallet = st.number_input('Points in Wallet', min_value=0, max_value=2070, value=600 ,step=1)
41
+ avg_transaction_value = st.number_input('Avg Transaction Value', min_value=800, max_value=90000, value=30000 ,step=1)
42
+ preferred_offer_types = st.radio('Offer Types', options=['Without Offers','Credit/Debit Card Offers','Gift Vouchers/Coupons'], horizontal=True)
43
+ used_special_discount = st.selectbox('Used Special Discount',('Yes','No'),index=1)
44
+ past_complaint = st.selectbox('Past Complaint',('Yes','No'),index=1)
45
+ feedback = st.selectbox('Feedback',('Poor Website','Poor Customer Service', 'Too many ads', 'Poor Product Quality', 'No reason specified', 'Products always in Stock', 'Reasonable Price', 'Quality Customer Care', 'User Friendly Website'),index=1)
46
+ submitted = st.form_submit_button('Predict')
47
+
48
+ # Create New Data
49
+ data_inf = {
50
+ 'user_id' : user_id,
51
+ 'age' : age,
52
+ 'gender' : gender,
53
+ 'region_category' : region_category,
54
+ 'internet_option' : internet_option,
55
+ 'medium_of_operation' : medium_of_operation,
56
+ 'days_since_last_login' : days_since_last_login,
57
+ 'avg_frequency_login_days' : avg_frequency_login_days,
58
+ 'joined_through_referral' : joined_through_referral,
59
+ 'membership_category' : membership_category,
60
+ 'points_in_wallet' : points_in_wallet,
61
+ 'avg_transaction_value' : avg_transaction_value,
62
+ 'used_special_discount' : used_special_discount,
63
+ 'past_complaint' : past_complaint,
64
+ 'preferred_offer_types' : preferred_offer_types,
65
+ 'feedback' : feedback
66
+ }
67
+
68
+ data_inf = pd.DataFrame([data_inf])
69
+ data_inf
70
+
71
+ if submitted :
72
+ # Feature Scaling and Feature Encoding
73
+ data_final = preprocessor.transform(data_inf)
74
+
75
+ # Predict using Linear Regression
76
+ y_inf_pred = np.where(model_churn.predict(data_final) >= 0.5, 1, 0)
77
+
78
+ if y_inf_pred == 1:
79
+ prediction = 'Churn'
80
+ else:
81
+ prediction = 'Not Churn'
82
+
83
+ st.write('##### This customer is predicted:', prediction)
84
+
85
+ if __name__ == '__main__':
86
+ run()
preprocessor_churn.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:53f9ce0813c933ea397ed161e771c8cf61281e54e1d41745100fb6636ed89aeb
3
+ size 3148
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ seaborn
4
+ matplotlib
5
+ tensorflow == 2.11.0
6
+ scikit-learn == 1.0.2
7
+ numpy
8
+ plotly