-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
112 lines (91 loc) · 3.35 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
from flask import make_response ,send_file,Flask ,redirect, url_for, request, render_template
import json
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression, LinearRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn import svm
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
import pandas as pd
@app.route("/")
@cross_origin()
def home_view():
return render_template("home.html")
@app.route("/getpred",methods=['POST','GET'])
@cross_origin()
def givePred():
if(request.method == 'POST'):
train = pd.read_csv(request.files['train'])
test = pd.read_csv(request.files['test'])
cols_train = train.columns
cols_test = test.columns
to_predict = list(set(cols_train)-set(cols_test))[0]
print("Predicting : ",to_predict)
X = train.drop(to_predict,axis = 1)
y = pd.DataFrame(train[to_predict])
#list of columns to drop
drops = []
convert = []
for colname in X.columns:
if(X[colname].dtype.name == 'object'):
if(len(X[colname].unique()) <= 2):
convert.append(colname)
else:
drops.append(colname)
elif(X[colname].isna().sum()/X[colname].shape[0]) > 0.2:
drops.append(colname)
print("Drops : ")
print(drops)
print("Conv : ")
print(convert)
for colname in drops:
X.drop(colname,axis = 1,inplace=True)
test.drop(colname,axis = 1,inplace=True)
X = X.apply(lambda row: row.fillna(row.mode()[0]), axis=1)
test = test.apply(lambda row: row.fillna(row.mode()[0]), axis=1)
for colname in convert:
type1 = X[colname].unique()[0]
X[colname] = [int(1) if type1 == i else int(0) for i in X[colname]]
test[colname] = [int(1) if type1 == i else int(0) for i in test[colname]]
for colname in X.columns:
X[colname] = pd.to_numeric(X[colname], errors='coerce')
test[colname] = pd.to_numeric(test[colname], errors='coerce')
'''
print("INFO X ")
X.info()
print("DESC X ")
X.describe()
print("INFO X ")
X.info()
print("DESC X ")
X.describe()
X.info()
y.info()
test.info()
'''
model_type = request.form['options']
if model_type == 'logistic':
model = LogisticRegression()
elif model_type == 'linear':
model = LinearRegression()
elif model_type == 'svm':
model = svm.SVC()
elif model_type == 'dtree':
model = DecisionTreeClassifier()
model.fit(X,y)
pred = model.predict(test)
pred = pd.DataFrame(pred)
print(pred)
#Returns a file with all the training data as well as the predictions
#Also NaN, Null values are filled
test[to_predict] = pred
pred = test
resp = make_response(pred.to_csv(index=False))
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp
if __name__ == "__main__":
app.run()