File size: 4,190 Bytes
c092e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import json
from datetime import date, datetime, time
from tensorflow.keras.models import load_model

# load model
with open('final_pipeline.pkl', 'rb') as file_1:
  model_pipeline = pickle.load(file_1)
model_ann = load_model('churn_model.h5')

def run():
  # membuat judul
  st.title('Churn Prediction')
  # membuat kolom input
  with st.form(key='Churn Status'):
      user_id = st.text_input('User ID', value='')
      age = st.number_input('Age', min_value=10, max_value=70, value=26, step=1)
      gender = st.radio('Gender', ('M','F'))
      region_category = st.selectbox('Region Category', ('Town','City','Village'),index=1)
      membership_category = st.selectbox('Membership Category', ('No Membership', 'Basic Membership', 'Silver Membership',  'Gold Membership',  'Premium Membership', 'Platinum Membership' ),index=1)
      joining_date = st.date_input('Joining Date', date.today())
      joined_through_referral = st.radio('Join By Referral', ('Yes','No'))
      preferred_offer_types = st.selectbox('Preferred Offer Types', ('Gift Vouchers/Coupons','Credit/Debit Card Offers', 'Without Offers'),index=1)
      medium_of_operation = st.selectbox('Medium of Operation', ('Desktop', 'Smartphone', 'Both'),index=1)
      internet_option = st.selectbox('Internet Option', ('Wi-Fi', 'Mobile_Data', 'Fiber_Optic'),index=1)
      last_visit_time = st.time_input('Last Visit Time ', time(hour=9, minute=0))
      days_since_last_login = st.number_input('Days Since Last Login', min_value=0, max_value=30, value=26, step=1)
      avg_time_spent = st.number_input('Average Time Spent', min_value=0, max_value=400, value=26, step=1)
      avg_transaction_value = st.number_input('Average Transaction Value ', min_value=750, max_value=100000, value=800, step=50)
      avg_frequency_login_days = st.number_input('Average Frequency Login Days', min_value=0, max_value=70, value=26, step=1)
      points_in_wallet = st.number_input('Points In Wallet', min_value=0, max_value=2100, value=260, step=10)
      used_special_discount = st.radio('Use Special Discount', ('Yes','No'))
      offer_application_preference = st.radio('Offer Application Preference', ('Yes','No'))
      past_complaint = st.radio('Past Complain', ('Yes','No'))
      complaint_status = st.selectbox('Complaint Status', ('Not Applicable','Unsolved', 'Solved', 'Solved in Follow-up', 'No Information Available'),index=1)
      feedback = st.selectbox('Feedback', ('Poor Product Quality','No reason specified', 'Too many ads', 'Poor Website', 'Poor Customer Service', 'Reasonable Price', 'User Friendly Website', 'Products always in Stock', 'Quality Customer Care'),index=1)

      submitted = st.form_submit_button('Predict')

  # membuat data-set baru
  data_inf = {
      'user_id':user_id,
      'age':age,
      'gender':gender,
      'region_category':region_category,
      'membership_category':membership_category,
      'joining_date':joining_date,
      'joined_through_referral':joined_through_referral,
      'preferred_offer_types': preferred_offer_types,
      'medium_of_operation': medium_of_operation,
      'internet_option': internet_option,
      'last_visit_time':last_visit_time,
      'days_since_last_login': days_since_last_login,
      'avg_time_spent': avg_time_spent,
      'avg_transaction_value':avg_transaction_value,
      'avg_frequency_login_days': avg_frequency_login_days,
      'points_in_wallet': points_in_wallet,
      'used_special_discount': used_special_discount,
      'offer_application_preference': offer_application_preference,
      'past_complaint': past_complaint,
      'complaint_status': complaint_status,
      'feedback': feedback
      }

  data_inf = pd.DataFrame([data_inf])
  st.dataframe(data_inf)

  if submitted:
    # transform inference-Set
    data_inf_transform = model_pipeline.transform(data_inf)

    # predict using neural network
    y_pred_inf = model_ann.predict(data_inf_transform)
    y_pred_inf = np.where(y_pred_inf >= 0.5, 1, 0)
    churn_prediction = "Yes" if y_pred_inf == 1 else "No"
    st.write('# Churn : ', churn_prediction)

if __name__ == '__main__': run()