-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
338 lines (310 loc) · 19.6 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from flask import Flask, render_template,request,redirect,url_for
import prof_r as pr
import linear_reg as l
import logistic_reg as ll
import decision_tree as dt
import naive_bayes as nb
import random_forest as rf
import random_forest2 as rf2
import knn as kn
import kmeans as km
import os
import pandas as pd
import pandas_profiling
import numpy as np
app=Flask(__name__)
UPLOAD_FOLDER = '/datasets'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ['csv']
@app.route('/')
def main_page():
return render_template('main_page.html')
@app.route('/linear_reg')
def linear_reg():
return render_template('linear_regression.html')
@app.route('/linear_reg',methods = ['POST', 'GET'])
def linear1():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>Linear Regression</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/linear_reg");</script></body></html>'
if not os.path.exists('static/Plots/Linear Regression/'+dataset_file_name):
os.mkdir('static/Plots/Linear Regression/'+dataset_file_name)
os.mkdir('static/Plots/Linear Regression/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/Linear Regression/'+dataset_file_name+"/Numerical Variables")
profile_report_path="static/Plots/Linear Regression/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,0)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('linear_regression.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
dataset=pd.read_csv(dataset)
dataset_file_name=request.form["dataset_file_name"]
r2,var,num_plots,cat_plots=l.linear_reg1(dataset,target,dataset_file_name)
return render_template('linear_regression.html',target=target,seq=2,var=var,r2=r2,num_plots=num_plots,cat_plots=cat_plots)
@app.route('/logistic_reg')
def logistic_reg():
return render_template('logistic_regression.html')
@app.route('/logistic_reg',methods = ['POST', 'GET'])
def logistic1():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>Logistic Regression</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/logistic_reg");</script></body></html>'
if not os.path.exists('static/Plots/Logistic Regression/'+dataset_file_name):
os.mkdir('static/Plots/Logistic Regression/'+dataset_file_name)
os.mkdir('static/Plots/Logistic Regression/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/Logistic Regression/'+dataset_file_name+"/Numerical Variables")
os.mkdir('static/Plots/Logistic Regression/'+dataset_file_name+"/Metrics")
profile_report_path="static/Plots/Logistic Regression/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,1)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('logistic_regression.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
no_of_labels=int(request.form["no_of_labels"])
dataset=pd.read_csv(dataset)
if target not in dataset.select_dtypes(include=['O']).columns.values:
if len(set(dataset[target]))!=no_of_labels:
return '<html><head><title>Logistic Regression</title></head><body><script>window.alert("The target variable must be categorical!");window.location.replace("/logistic_reg");</script></body></html>'
dataset_file_name=request.form["dataset_file_name"]
acc,f1,precision,recall,num_plots,cat_plots,conf_mat=ll.logistic_reg1(dataset,target,dataset_file_name)
return render_template('logistic_regression.html',target=target,seq=2,acc=acc,precision=precision,recall=recall,f1=f1,num_plots=num_plots,cat_plots=cat_plots,conf_mat=conf_mat)
@app.route('/decision_tree')
def decision_tree():
return render_template('decision_tree.html')
@app.route('/decision_tree',methods = ['POST', 'GET'])
def decision1():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>Decision Tree</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/decision_tree");</script></body></html>'
if not os.path.exists('static/Plots/Decision Tree/'+dataset_file_name):
os.mkdir('static/Plots/Decision Tree/'+dataset_file_name)
os.mkdir('static/Plots/Decision Tree/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/Decision Tree/'+dataset_file_name+"/Numerical Variables")
os.mkdir('static/Plots/Decision Tree/'+dataset_file_name+"/Metrics")
os.mkdir('static/Plots/Decision Tree/'+dataset_file_name+"/Decision Tree")
profile_report_path="static/Plots/Decision Tree/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,1)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('decision_tree.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
no_of_labels=int(request.form["no_of_labels"])
dataset=pd.read_csv(dataset)
if target not in dataset.select_dtypes(include=['O']).columns.values:
if len(set(dataset[target]))!=no_of_labels:
return '<html><head><title>Decision Regression</title></head><body><script>window.alert("The target variable must be categorical!");window.location.replace("/decision_tree");</script></body></html>'
dataset_file_name=request.form["dataset_file_name"]
acc,f1,precision,recall,num_plots,cat_plots,conf_mat,dtree=dt.decision_tree1(dataset,target,dataset_file_name)
return render_template('decision_tree.html',target=target,seq=2,acc=acc,precision=precision,recall=recall,f1=f1,num_plots=num_plots,cat_plots=cat_plots,conf_mat=conf_mat,dtree=dtree)
@app.route('/naive_bayes')
def naive_bayes():
return render_template('naive_bayes.html')
@app.route('/naive_bayes',methods = ['POST', 'GET'])
def naive1():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>Naive Bayes</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/naive_bayes");</script></body></html>'
if not os.path.exists('static/Plots/Naive Bayes/'+dataset_file_name):
os.mkdir('static/Plots/Naive Bayes/'+dataset_file_name)
os.mkdir('static/Plots/Naive Bayes/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/Naive Bayes/'+dataset_file_name+"/Numerical Variables")
os.mkdir('static/Plots/Naive Bayes/'+dataset_file_name+"/Metrics")
profile_report_path="static/Plots/Naive Bayes/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,1)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('naive_bayes.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
no_of_labels=int(request.form["no_of_labels"])
dataset=pd.read_csv(dataset)
if target not in dataset.select_dtypes(include=['O']).columns.values:
if len(set(dataset[target]))!=no_of_labels:
return '<html><head><title>Naive Bayes</title></head><body><script>window.alert("The target variable must be categorical!");window.location.replace("/naive_bayes");</script></body></html>'
dataset_file_name=request.form["dataset_file_name"]
acc,f1,precision,recall,num_plots,cat_plots,conf_mat=nb.naive_bayes1(dataset,target,dataset_file_name)
return render_template('naive_bayes.html',target=target,seq=2,acc=acc,precision=precision,recall=recall,f1=f1,num_plots=num_plots,cat_plots=cat_plots,conf_mat=conf_mat)
@app.route('/random_forest')
def random_forest():
return render_template('random_forest.html')
@app.route('/random_forest',methods = ['POST', 'GET'])
def random1():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>Random Forest</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/random_forest");</script></body></html>'
if not os.path.exists('static/Plots/Random Forest/'+dataset_file_name):
os.mkdir('static/Plots/Random Forest/'+dataset_file_name)
os.mkdir('static/Plots/Random Forest/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/Random Forest/'+dataset_file_name+"/Numerical Variables")
os.mkdir('static/Plots/Random Forest/'+dataset_file_name+"/Metrics")
profile_report_path="static/Plots/Random Forest/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,1)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('random_forest.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
no_of_labels=int(request.form["no_of_labels"])
dataset=pd.read_csv(dataset)
if target not in dataset.select_dtypes(include=['O']).columns.values:
if len(set(dataset[target]))!=no_of_labels:
return '<html><head><title>Random Forest</title></head><body><script>window.alert("The target variable must be categorical!");window.location.replace("/random_forest");</script></body></html>'
dataset_file_name=request.form["dataset_file_name"]
acc,f1,precision,recall,num_plots,cat_plots,conf_mat,mae=rf.random_forest1(dataset,target,dataset_file_name)
return render_template('random_forest.html',target=target,seq=2,acc=acc,precision=precision,recall=recall,f1=f1,num_plots=num_plots,cat_plots=cat_plots,conf_mat=conf_mat,mae=mae)
@app.route('/Knn')
def Knn():
return render_template('Knn.html')
@app.route('/Knn',methods = ['POST', 'GET'])
def Knn1():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>k-Nearest Neighbours</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/Knn");</script></body></html>'
if not os.path.exists('static/Plots/Knn/'+dataset_file_name):
os.mkdir('static/Plots/Knn/'+dataset_file_name)
os.mkdir('static/Plots/Knn/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/Knn/'+dataset_file_name+"/Numerical Variables")
os.mkdir('static/Plots/Knn/'+dataset_file_name+"/Metrics")
profile_report_path="static/Plots/Knn/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,1)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('Knn.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
no_of_labels=int(request.form["no_of_labels"])
dataset=pd.read_csv(dataset)
if target not in dataset.select_dtypes(include=['O']).columns.values:
if len(set(dataset[target]))!=no_of_labels:
return '<html><head><title>k-Nearest Neighbours</title></head><body><script>window.alert("The target variable must be categorical!");window.location.replace("/Knn");</script></body></html>'
dataset_file_name=request.form["dataset_file_name"]
acc,f1,precision,recall,num_plots,cat_plots,conf_mat,er,ac_vs_k=kn.knn1(dataset,target,dataset_file_name)
return render_template('Knn.html',target=target,seq=2,acc=acc,precision=precision,recall=recall,f1=f1,num_plots=num_plots,cat_plots=cat_plots,conf_mat=conf_mat,er=er,ac_vs_k=ac_vs_k)
@app.route('/kmeans')
def Kmeans():
return render_template('kmeans.html')
@app.route('/kmeans',methods = ['POST', 'GET'])
def k_means1():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>K-Means</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/kmeans");</script></body></html>'
if not os.path.exists('static/Plots/K-means/'+dataset_file_name):
os.mkdir('static/Plots/K-means/'+dataset_file_name)
os.mkdir('static/Plots/K-means/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/K-means/'+dataset_file_name+"/Numerical Variables")
os.mkdir('static/Plots/K-means/'+dataset_file_name+"/Metrics")
profile_report_path="static/Plots/K-means/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,1)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('kmeans.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
no_of_labels=int(request.form["no_of_labels"])
dataset=pd.read_csv(dataset)
if target not in dataset.select_dtypes(include=['O']).columns.values:
if len(set(dataset[target]))!=no_of_labels:
return '<html><head><title>K-Means</title></head><body><script>window.alert("The target variable must be categorical!");window.location.replace("/kmeans");</script></body></html>'
dataset_file_name=request.form["dataset_file_name"]
nmi,ars,ami,cat_plots,conf_mat,scat_plots,wc_plot=km.k_means1(dataset,target,dataset_file_name)
return render_template('kmeans.html',target=target,seq=2,nmi=nmi,ars=ars,ami=ami,scat_plots=scat_plots,cat_plots=cat_plots,conf_mat=conf_mat,wc_plot=wc_plot)
@app.route('/random_forest2')
def random_forest2():
return render_template('random_forest2.html')
@app.route('/random_forest2',methods = ['POST', 'GET'])
def random2():
if "target" not in request.form:
file=request.files['dataset']
if allowed_file(file.filename):
file.save(os.path.join("static/datasets/", file.filename))
dataset_file_path="static/datasets/"+file.filename
dataset=pd.read_csv(dataset_file_path)
dataset_file_name=file.filename[:-4]
else:
return '<html><head><title>Random Forest Regression</title></head><body><script>window.alert("The file uploaded should be csv file");window.location.replace("/random_forest2");</script></body></html>'
if not os.path.exists('static/Plots/Random Forest/'+dataset_file_name):
os.mkdir('static/Plots/Random Forest/'+dataset_file_name)
os.mkdir('static/Plots/Random Forest/'+dataset_file_name+"/Categorical Variables")
os.mkdir('static/Plots/Random Forest/'+dataset_file_name+"/Numerical Variables")
os.mkdir('static/Plots/Random Forest/'+dataset_file_name+"/Metrics")
profile_report_path="static/Plots/Random Forest/"+dataset_file_name+"/profile_report.html"
cols=pr.prof(dataset,profile_report_path,0)
profile_report_path=profile_report_path.replace(" ","%20")
return render_template('random_forest2.html',dataset=dataset_file_path,cols=cols,profile_report_path=profile_report_path,dataset_file_name=dataset_file_name)
else:
target=request.form["target"]
dataset=request.form["dataset"]
dataset=pd.read_csv(dataset)
dataset_file_name=request.form["dataset_file_name"]
mae,var_score,r2_score,num_plots,cat_plots,conf_mat=rf2.random_forest2(dataset,target,dataset_file_name)
return render_template('random_forest2.html',target=target,seq=2,mae=mae,var_score=var_score,r2_score=r2_score,num_plots=num_plots,cat_plots=cat_plots,conf_mat=conf_mat)
@app.route('/learn_more')
def learn_more():
return render_template('learn_more.html')
@app.route('/learn_linear')
def learn_linear():
return render_template('learn_x.html',filename="static/Linear Regression.PNG",name="Linear Regression")
@app.route('/learn_logistic')
def learn_logistic():
return render_template('learn_x.html',filename="static/Logistic Regression.PNG",name="Logistic Regression")
@app.route('/learn_decision')
def learn_decision():
return render_template('learn_x.html',filename="static/Decision Tree.PNG",name="Decision Tree")
@app.route('/learn_random')
def learn_random():
return render_template('learn_x.html',filename="static/Random Forest.PNG",name="Random Forest")
@app.route('/learn_kmeans')
def learn_kmeans():
return render_template('learn_x.html',filename="static/K Means.PNG",name="K Means")
@app.route('/learn_knn')
def learn_knn():
return render_template('learn_x.html',filename="static/Knn.PNG",name="k-NN")
if __name__ == '__main__':
app.run()