-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchurn.py
111 lines (78 loc) · 3.22 KB
/
churn.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
from flask import Flask, request, jsonify
import joblib
import traceback
import pandas as pd
import numpy as np
# Your API definition
app = Flask(__name__)
@app.route('/churn/predict', methods=['POST'])
def churnpredict():
model1 = joblib.load("randomforestchurnmodel.pkl") # Load "model.pkl"
print ('Model loaded')
model_columns = joblib.load("model_columns.pkl") # Load "model_columns.pkl"
print ('Model columns loaded')
if model1:
try:
json_ = request.json
print(json_)
query = pd.get_dummies(pd.DataFrame(json_))
query = query.reindex(columns=model_columns, fill_value=0)
prediction = model1.predict_proba(query)
output = prediction[:,0]*100
a_list = list(output)
print(a_list[0])
a= round(a_list[0], 2)
return jsonify({'The probability of a customer becoming defaulter is': str(a)})
except:
return jsonify({'trace': traceback.format_exc()})
else:
print ('Train the model first')
return ('No model here to use')
@app.route('/homeloan/predict', methods=['POST'])
def homeloanpredict():
model2= joblib.load("logistic_regression_homeloan_model.pkl") # Load "model.pkl"
print ('Model loaded')
model_columns = joblib.load("homeloanmodel_columns.pkl") # Load "model_columns.pkl"
print ('Model columns loaded')
if model2:
try:
json_ = request.json
print(json_)
query = pd.get_dummies(pd.DataFrame(json_))
query = query.reindex(columns=model_columns, fill_value=0)
prediction = model2.predict_proba(query)
output = prediction[:,1]*100
a_list = list(output)
print(a_list[0])
a= round(a_list[0], 2)
return jsonify({'The probability of a customer getting home loan ': str(a)})
except:
return jsonify({'trace': traceback.format_exc()})
else:
print ('Train the model first')
return ('No model here to use')
@app.route('/personalloan/predict', methods=['POST'])
def personalloanpredict():
model3 = joblib.load("logistic_regression_personalloan.pkl") # Load "model.pkl"
print ('Model loaded')
model_columns = joblib.load("personalloanmodel_columns.pkl") # Load "model_columns.pkl"
print ('Model columns loaded')
if model3:
try:
json_ = request.json
print(json_)
query = pd.get_dummies(pd.DataFrame(json_))
query = query.reindex(columns=model_columns, fill_value=0)
prediction = model3.predict_proba(query)
output = prediction[:,1]*100
a_list = list(output)
print(a_list[0])
a= round(a_list[0], 2)
return jsonify({'The probability of a customer getting personal loan ': str(a)})
except:
return jsonify({'trace': traceback.format_exc()})
else:
print ('Train the model first')
return ('No model here to use')
if __name__ == '__main__':
app.run("0.0.0.0",port=5000, debug=True)