File size: 4,438 Bytes
110b1f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import json
import tensorflow as tf
# Load All Files

# Load the Models

with open('final_pipeline.pkl', 'rb') as file_1:
  model_pipeline = pickle.load(file_1)

with open('model_encoder.pkl','rb') as file_2:
  encoder_ord = pickle.load(file_2)

model_ann = tf.keras.models.load_model('churn_model.h5')


def run():
  with st.form(key='from_churn'):
      user_id = st.text_input('User id', value='')
      age = st.number_input('Age', min_value=0, max_value=100, value=0)
      gender = st.selectbox('Gender', ('M', 'F'), index=1)
      region_category = st.selectbox('Region', ('Town', 'City', 'Village'), index=1)
      st.markdown('---')

      membership_category = st.selectbox('Membership Category', ('Basic Membership', 'No Membership', 'Gold Membership', 'Silver Membership', 'Premium Membership', 'Platinum Membership'), index=1)
      joining_days = st.number_input('joining_days', min_value=0, max_value=1000, value=0)
      joined_through_referral = st.selectbox('Join Through Referral', ('Yes', 'No'), index=1)
      preferred_offer_types = st.selectbox('Offer Type', ('Gift Vouchers/Coupons', 'Credit/Debit Card Offers', 'Without Offers'), index=1)
      medium_of_operation = st.selectbox('Gadget Type', ('Desktop', 'Smartphone', 'Both'), index=1)
      internet_option = st.selectbox('Internet Type', ('Wi-Fi', 'Mobile_Data', 'Fiber_Optic'), index=1)
      days_since_last_login = st.number_input('Days Since Last Login', min_value=0, max_value=100, value=0)
      avg_time_spent = st.number_input('Average Time Spent', min_value=0, max_value=3000, value=0)
      avg_transaction_value = st.number_input('Average Transaction Value', min_value=0, max_value=100000, value=0)
      avg_frequency_login_days = st.number_input('Average Login Days', min_value=0, max_value=100, value=0)
      points_in_wallet = st.number_input('Points in Wallet', min_value=0, max_value=3000, value=0)
      used_special_discount = st.selectbox('Used Special Discount', ('Yes', 'No'), index=1)
      offer_application_preference = st.selectbox('Offer Preference', ('Yes', 'No'), index=1)
      past_complaint = st.selectbox('Past Complaint', ('Yes', 'No'), index=1)
      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')

  data_inf = {
      'user_id': user_id,
      'age': age,
      'gender': gender,
      'region_category': region_category,
      'membership_category': membership_category,
      'joining_days': joining_days,
      'joined_through_referral': joined_through_referral,
      'preferred_offer_types': preferred_offer_types,
      'medium_of_operation': medium_of_operation,
      'internet_option': internet_option,
      '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:
    # Split between Numerical Columns and Categorical Columns
    enc_columns = ['membership_category']
    data_inf[enc_columns] = encoder_ord.fit_transform(data_inf[enc_columns])

    # Feature Scaling and Feature Encoding
    data_inf_transform = model_pipeline.transform(data_inf)
    
    #data_inf_num_scaled = model_scaler.transform(data_inf_num)
    #data_inf_cat_encoded = model_encoder.transform(data_inf_cat)
    #data_inf_final = np.concatenate([data_inf_num_scaled, data_inf_cat_encoded], axis=1)

    # Predict using Linear Regression
    y_pred_inf = model_ann.predict(data_inf_transform)
    y_pred_inf = np.where(y_pred_inf >= 0.5, 1, 0)

    st.write('# Churn Risk : ', str(int(y_pred_inf)))

if __name__=='__main__':
    run()