-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
119 lines (106 loc) · 4.19 KB
/
app.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
"""Makes prediction how high a candidate's chnage is to get elected into parliament"""
from flask import Flask, render_template, request, jsonify
import joblib
import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
# Env is flask-test
app = Flask(__name__)
model = joblib.load(open('model.pkl', 'rb'))
def budgetRace(inputArray):
if inputArray[-2] == 0:
inputArray[-2] = 500
elif inputArray[-2] == 1:
inputArray[-2] = 3000
elif inputArray[-2] == 2:
inputArray[-2] = 7500
elif inputArray[-2] == 3:
inputArray[-2] = 15000
elif inputArray[-2] == 4:
inputArray[-2] = 35000
elif inputArray[-2] == 5:
inputArray[-2] = 90000
else:
inputArray[-2] = 15000
def IncomeRace(inputArray):
if inputArray[-1] == 0:
inputArray[-1] = 10000
elif inputArray[-1] == 1:
inputArray[-1] = 25000
elif inputArray[-1] == 2:
inputArray[-1] = 40000
elif inputArray[-1] == 3:
inputArray[-1] = 60000
elif inputArray[-1] == 4:
inputArray[-1] = 85000
elif inputArray[-1] == 5:
inputArray[-1] = 130000
else:
inputArray[-1] = 40000
def predResult(probInput):
basetext = "Change to get elected is "
if probInput < 30:
return basetext + "very low (score 1/5)"
elif probInput < 45:
return basetext + "quite low (score 2/5)"
elif probInput < 55:
return basetext + "modest (score 3/5)"
elif probInput < 65:
return basetext + "quite high (score 4/5)"
else:
return basetext + "high (score 5/5)"
@app.route('/')
def home():
return render_template('index.html')
@app.route("/form")
def form():
return render_template('form.html')
@app.route('/form-handler', methods=['POST'])
def handle_data():
return jsonify(request.form)
@app.route('/predict', methods=['POST', 'GET'])
def predict():
int_features = [int(x) for x in request.form.values()]
budgetRace(int_features)
IncomeRace(int_features)
mylist0 = [32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 500, 10000]
mylist1 = [32, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3000, 25000]
mylist2 = [32, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, 7500, 40000]
mylist3 = [32, 1, 1, 1, 3, 0, 1, 1, 3, 3, 0, 1, 1, 15000, 60000]
mylist4 = [32, 1, 1, 1, 1, 0, 1, 1, 2, 4, 0, 1, 1, 35000, 85000]
mylist5 = [32, 1, 1, 1, 1, 0, 1, 1, 0, 5, 0, 1, 1, 90000, 130000]
mylist6 = [32, 1, 1, 1, 1, 0, 1, 1, 1, 6, 0, 1, 1, 500, 10000]
mylist7 = [32, 1, 1, 1, 1, 0, 1, 1, 1, 7, 0, 1, 1, 500, 10000]
mylist8 = [32, 1, 1, 1, 1, 0, 1, 1, 1, 8, 0, 1, 1, 500, 10000]
mydataf = pd.DataFrame([int_features,
mylist0,
mylist1,
mylist2,
mylist3,
mylist4,
mylist5,
mylist6,
mylist7,
mylist8],
columns=['age',
'sex', 'celebrity', 'currently_in_parliament', 'education',
'mother_tongue', 'twitter_account', 'children', 'employer',
'work_status', 'languages_known', 'PM_party',
'external_election_funding', 'elect_budget_new ', 'yearly_income_new'])
crit1 = mydataf.dtypes != object
cat2 = mydataf.columns[crit1].tolist()
cat3 = cat2[4:10]
cat3.remove('children')
cat3.remove('twitter_account')
OH_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
OH_cols_train_user = pd.DataFrame(OH_encoder.fit_transform(mydataf[cat3]))
OH_cols_train_user.index = mydataf.index
num_X_train2 = mydataf.drop(cat3, axis=1)
OH_X_user = pd.concat([num_X_train2, OH_cols_train_user], axis=1)
userd_newformat = pd.DataFrame(model.predict_proba(OH_X_user.values))
probability = np.around((userd_newformat.iloc[0, 1])*100, decimals=2)
outcome = predResult(probability)
# return jsonify(output)
return render_template('result.html', prediction_text=outcome)
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)