-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
100 lines (89 loc) · 3.32 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
import numpy as np
import pandas as pd
from flask import Flask, request, jsonify, render_template
import joblib
import json
app = Flask(__name__)
model_columns = joblib.load("model_columns.pkl")
logreg = joblib.load('models/model_logreg.pkl')
#knn = joblib.load('models/model_knn.pkl')
svc = joblib.load('models/model_svm.pkl')
#decision = joblib.load('models/model_decision.pkl')
#random_forest = joblib.load('models/model_random_forest.pkl')
gaussian = joblib.load('models/model_gaussiannb.pkl')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
d = None
if request.method == 'POST':
print('POST Received.')
d = request.form.to_dict()
else:
print('GET Received.')
d = request.args.to_dict()
#d: Pclass, Sex, Age, Fare, Embarked, Title[Name], IsAlone[Parch, Sibsp],
table = pd.DataFrame([d])
'''
Pclass Sex Age Fare Embarked Title Name IsAlone
0 first male 21 322 C dsdSD 1
|
|
|
'''
#['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', 'Title', 'IsAlone', 'Age*Class']
#Age column
if int(table['Age'])<=16:
table['Age'] = 0
elif int(table['Age']) > 16 and int(table['Age']) <= 32:
table['Age'] = 1
elif int(table['Age']) > 32 and int(table['Age']) <= 48:
table['Age'] = 2
elif int(table['Age']) > 48 and int(table['Age']) <= 64:
table['Age'] = 3
elif int(table['Age']) > 64:
table['Age'] = 4
if int(table['Fare']) <= 7.91:
table['Fare'] = 0
elif int(table['Fare']) > 7.91 and int(table['Fare']) <= 14.454:
table['Fare'] = 1
elif int(table['Fare']) > 14.454 and int(table['Fare']) <= 31:
table['Fare'] = 2
elif int(table['Fare']) > 31:
table['Fare'] = 3
table['Fare'] = table['Fare'].astype(int)
#New column by feature engineering
table['Age*Class'] = int(table['Age'].item()) * int(table['Pclass'].item())
model = table['Model'].item()
print(model)
print(table)
table = table.drop("Model", axis=1)
query = table.reindex(columns=model_columns)
print(query)
if model=='logreg':
prediction = logreg.predict(query)
#if model=='knn':
#prediction = knn.predict(query)
if model=='svc':
prediction = svc.predict(query)
#if model=='decision':
#prediction = decision.predict(query)
#if model=='random_forest':
#prediction = random_forest.predict(query)
if model=='gaussian':
prediction = gaussian.predict(query)
if prediction==[0]:
output = 'would have died. Geez!'
elif prediction==[1]:
output = 'would have survived. Phew!!'
return render_template('index.html', prediction_text='You {}'.format(output))
if __name__ == '__main__':
logreg = joblib.load('models/model_logreg.pkl')
#knn = joblib.load('models/model_knn.pkl')
svc = joblib.load('models/model_svm.pkl')
#decision = joblib.load('models/model_decision.pkl')
#random_forest = joblib.load('models/model_random_forest.pkl')
gaussian = joblib.load('models/model_gaussiannb.pkl')
model_columns = joblib.load("model_columns.pkl")
app.run(debug=True)