-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp.py
131 lines (104 loc) · 4.94 KB
/
temp.py
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
# coding: utf-8
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
from flask import Flask, request, render_template
import pickle
app = Flask("__name__")
df_1=pd.read_csv("tel_churn.csv")
q = ""
@app.route("/")
def loadPage():
return render_template('home.html', query="")
@app.route("/", methods=['POST'])
def predict():
'''
SeniorCitizen
MonthlyCharges
TotalCharges
gender
Partner
Dependents
PhoneService
MultipleLines
InternetService
OnlineSecurity
OnlineBackup
DeviceProtection
TechSupport
StreamingTV
StreamingMovies
Contract
PaperlessBilling
PaymentMethod
tenure
'''
inputQuery1 = request.form['query1']
inputQuery2 = request.form['query2']
inputQuery3 = request.form['query3']
inputQuery4 = request.form['query4']
inputQuery5 = request.form['query5']
inputQuery6 = request.form['query6']
inputQuery7 = request.form['query7']
inputQuery8 = request.form['query8']
inputQuery9 = request.form['query9']
inputQuery10 = request.form['query10']
inputQuery11 = request.form['query11']
inputQuery12 = request.form['query12']
inputQuery13 = request.form['query13']
inputQuery14 = request.form['query14']
inputQuery15 = request.form['query15']
inputQuery16 = request.form['query16']
inputQuery17 = request.form['query17']
inputQuery18 = request.form['query18']
inputQuery19 = request.form['query19']
model = pickle.load(open("model.sav", "rb"))
data = [[inputQuery1, inputQuery2, inputQuery3, inputQuery4, inputQuery5, inputQuery6, inputQuery7,
inputQuery8, inputQuery9, inputQuery10, inputQuery11, inputQuery12, inputQuery13, inputQuery14,
inputQuery15, inputQuery16, inputQuery17, inputQuery18, inputQuery19]]
new_df = pd.DataFrame(data, columns = ['SeniorCitizen', 'MonthlyCharges', 'TotalCharges', 'gender',
'Partner', 'Dependents', 'PhoneService', 'MultipleLines', 'InternetService',
'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport',
'StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling',
'PaymentMethod', 'tenure'])
df_2 = pd.concat([df_1, new_df], ignore_index = True)
# Group the tenure in bins of 12 months
labels = ["{0} - {1}".format(i, i + 11) for i in range(1, 72, 12)]
df_2['tenure_group'] = pd.cut(df_2.tenure.astype(int), range(1, 80, 12), right=False, labels=labels)
#drop column customerID and tenure
df_2.drop(columns= ['tenure'], axis=1, inplace=True)
new_df__dummies = pd.get_dummies(df_2[['gender', 'SeniorCitizen', 'Partner', 'Dependents', 'PhoneService',
'MultipleLines', 'InternetService', 'OnlineSecurity', 'OnlineBackup',
'DeviceProtection', 'TechSupport', 'StreamingTV', 'StreamingMovies',
'Contract', 'PaperlessBilling', 'PaymentMethod','tenure_group']])
#final_df=pd.concat([new_df__dummies, new_dummy], axis=1)
single = model.predict(new_df__dummies.tail(1))
probablity = model.predict_proba(new_df__dummies.tail(1))[:,1]
if single==1:
o1 = "This customer is likely to be churned!!"
o2 = "Confidence: {}".format(probablity*100)
else:
o1 = "This customer is likely to continue!!"
o2 = "Confidence: {}".format(probablity*100)
return render_template('home.html', output1=o1, output2=o2,
query1 = request.form['query1'],
query2 = request.form['query2'],
query3 = request.form['query3'],
query4 = request.form['query4'],
query5 = request.form['query5'],
query6 = request.form['query6'],
query7 = request.form['query7'],
query8 = request.form['query8'],
query9 = request.form['query9'],
query10 = request.form['query10'],
query11 = request.form['query11'],
query12 = request.form['query12'],
query13 = request.form['query13'],
query14 = request.form['query14'],
query15 = request.form['query15'],
query16 = request.form['query16'],
query17 = request.form['query17'],
query18 = request.form['query18'],
query19 = request.form['query19'])
app.run()