-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchurn_API.py
60 lines (47 loc) · 1.84 KB
/
churn_API.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
import pandas as pd
from flask import Flask, jsonify, request
import pickle
import numpy as np
#The function will create the derived variable needed for model score..
def create_cpm(df):
charge_vars = [x for x in df.columns if 'charge' in x]
minutes_vars = [x for x in df.columns if 'minutes' in x]
df['total_charges'] = 0
df['total_minutes'] = 0
for indexer in range(0, len(charge_vars)):
df['total_charges'] += df[charge_vars[indexer]]
df['total_minutes'] += df[minutes_vars[indexer]]
df['charge_per_minute'] = np.where(df['total_minutes'] >0, df['total_charges']/df['total_minutes'], 0)
df.drop(['total_minutes', 'total_charges' ], axis = 1, inplace = True)
return df
# load model
model = pickle.load(open('model.pkl','rb'))
#load column order..
model_columns = pickle.load(open('model_columns.pkl','rb'))
# app
app = Flask(__name__)
# routes
@app.route('/', methods=['POST'])
def predict():
# get data
data = request.get_json(force=True)
# convert data into dataframe
data.update((x, [y]) for x, y in data.items())
data_df = pd.DataFrame.from_dict(data)
#adding the required derived and modified columns
data_df = create_cpm(data_df)
data_df['international_plan_num'] = data_df['international_plan'].apply(lambda x : 1 if x == 'yes' else 0)
data_df = data_df.reindex(columns=model_columns, fill_value=0)#added by SS
# predictions
result = model.predict_proba(data_df)[:, 1]
result2 = 0
if result[0] >=0.5:
result2 = 'yes'
else:
result2 = 'no'
# send back to browser
output = {'Model_score': result[0], 'Churn' : result2, 'cpm' : data_df['charge_per_minute'][0] }
# return data
return jsonify(results=output)
if __name__ == '__main__':
app.run(port = 5000, debug=True)