-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
387 lines (312 loc) · 11.1 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#import of packages, Libraries and modules.
from flask import Flask,render_template, request, flash, url_for,jsonify
import pandas as pd
import numpy as np
from flask import json
from sklearn import cross_validation,preprocessing
from sklearn.linear_model import LinearRegression
from sklearn.externals import joblib
from sklearn.ensemble import RandomForestRegressor
from plotly.offline import init_notebook_mode, iplot
#Starting of flask app
app = Flask(__name__)
@app.route('/')
def Index():
return render_template("home.html")
@app.route("/home.html")
def Home():
return render_template("home.html")
@app.route('/pred.html')
def pred():
return render_template("pred.html")
@app.route('/vis.html')
def viz():
return render_template("vis.html")
@app.route('/womenViz.html')
def womenViz():
return render_template('womenViz.html')
@app.route('/childrenViz.html')
def childrenViz():
return render_template('childrenViz.html')
@app.route('/IPCViz.html')
def IPCViz():
return render_template('IPCViz.html')
@app.route('/highlights.html')
def highlights():
return render_template("highlights.html")
@app.route('/women.html',methods = ['POST'])
def women():
year = request.form.get("Predict_Year") #Year fetching From UI.
C_type = request.form.get("C_Type") #Crime type fetching from UI
state = request.form.get("state") #State name fetching from UI
df = pd.read_csv("static/StateWiseCAWPred1990-2016.csv", header=None)
data1 = df.loc[df[0]==state].values #Selecting State and its attributes.
for x in data1:
if x[1] == C_type:
test = x
break
l = len(df.columns)
trendChangingYear = 2
xTrain = np.array([1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016])
yTrain = test[2:29]
X = df.iloc[0,2:l].values
y = test[2:]
regressor = LinearRegression() #regression algorithm cealled.
regressor.fit(X.reshape(-1,1),y) #Data set is fitted in regression and Reshaped it.
accuracy = regressor.score(X.reshape(-1,1),y) #Finding Accuracy of Predictions.
print accuracy
accuracy_max = 0.65
#Trending year(Influence Year) finding algorithm.
if(accuracy < 0.65): #Used 65% accuracy as benchmark for trending year finding algorithm.
for a in range(3,l-8):
X = df.iloc[0,a:l].values
y = test[a:]
regressor = LinearRegression()
regressor.fit(X.reshape(-1,1),y)
accuracy = regressor.score(X.reshape(-1,1),y)
if (accuracy > accuracy_max):
accuracy_max = accuracy
print accuracy_max
trendChangingYear = a
print trendChangingYear #Printing Trend Changing Year on server terminal.
print test[trendChangingYear]
print xTrain[trendChangingYear-2]
year = int(year)
y = test[2:]
b = []
#If accuracy is Lower than 65%, only visualization of the data is shown - no predictions
if accuracy < 0.65:
for k in range(2001,2017):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
msg = "Data is not Sutaible for prediction"
#Else predictions are shown and Run time data and labels are added to the graph.
else:
for j in range(2017,year+1):
prediction = regressor.predict(j)
if(prediction < 0):
prediction = 0
y = np.append(y,prediction)
y = np.append(y,0)
for k in range(1990,year+1):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
msg = ""
if C_type == "ASSAULT ON WOMEN WITH INTENT TO OUTRAGE HER MODESTY":
C_type = "ASSAULT ON WOMEN"
#Finally the template is rendered
return render_template('women.html',data = [accuracy,yTrain,xTrain,state,year,data1,X,y,test,l],msg = msg,state=state, year=year, C_type=C_type,pred_data = y,years = yearLable)
@app.route('/children.html',methods = ['POST'])
def children():
year = request.form.get("Predict_Year") #Year fetching From UI.
C_type = request.form.get("C_Type") #Crime type fetching from UI
state = request.form.get("state") #State name fetching from UI
#reading CSV file.
df = pd.read_csv("static/Statewise Cases Reported of Crimes Committed Against Children 1994-2016.csv", header=None)
data1 = df.loc[df[0]==state].values #Selecting State and its attributes.
for x in data1:
if x[1] == C_type:
test = x
break
l = len(df.columns)
trendChangingYear = 2
accuracy_max = 0.65
# Year array for Javascript for Labeling to the Graph
xTrain = np.array([1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016])
yTrain = test[2:25]
X = df.iloc[0,2:l].values
y = test[2:]
regressor = LinearRegression() #regression Algorithm Called.
regressor.fit(X.reshape(-1,1),y) #Data set is fitted in regression and Reshaped it.
accuracy = regressor.score(X.reshape(-1,1),y) #Finding Accuracy of Prdictions.
print accuracy
accuracy_max = 0.65
if(accuracy < 0.65):
for a in range(3,l-4):
X = df.iloc[0,a:l].values
y = test[a:]
regressor = LinearRegression()
regressor.fit(X.reshape(-1,1),y)
accuracy = regressor.score(X.reshape(-1,1),y)
if (accuracy > accuracy_max):
accuracy_max = accuracy
print accuracy_max
trendChangingYear = a
print trendChangingYear #Printing Trend Changing Year on server terminal.
print test[trendChangingYear]
print xTrain[trendChangingYear-2]
yTrain = test[trendChangingYear:]
xTrain = xTrain[trendChangingYear-2:]
regressor.fit(xTrain.reshape(-1,1),yTrain)
accuracy = regressor.score(xTrain.reshape(-1,1),yTrain)
year = int(year)
y = test[2:]
b = []
if accuracy < 0.65:
for k in range(2001,2017):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
year = 2016
msg = "Data is not Suitable for prediction"
else:
for j in range(2017,year+1):
prediction = regressor.predict(j)
if(prediction < 0):
prediction = 0
y = np.append(y,prediction)
y = np.append(y,0)
for k in range(1994,year+1):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
msg = ""
return render_template('children.html',data = [accuracy,yTrain,xTrain,state,year,data1,X,y,test,l],state=state, year=year,msg=msg, C_type=C_type,pred_data = y,years = yearLable)
@app.route('/ipc.html',methods = ['POST'])
def ipc():
year = request.form.get("Predict_Year") #Year fetching From UI.
C_type = request.form.get("C_Type") #Crime type fetching from UI
state = request.form.get("state") #State name fetching from UI
#reading CSV file.
df = pd.read_csv("static/StateIPCPred2001_16.csv", header=None)
data1 = df.loc[df[0]==state].values #Selecting State and its attributes.
for x in data1:
if x[1] == C_type:
test = x
break
l = len(df.columns)
trendChangingYear = 2
accuracy_max = 0.65
# Year array for Javascript for Labeling to Graph
xTrain = np.array([2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016])
yTrain = test[2:18]
X = df.iloc[0,2:l].values
y = test[2:]
regressor = LinearRegression() #regression Algorithm Called.
regressor.fit(X.reshape(-1,1),y) #Data set is fitted in regression and Reshaped it.
accuracy = regressor.score(X.reshape(-1,1),y) #Finding Accuracy of Prdictions.
print accuracy
accuracy_max = 0.65
#Trending year(Influence Year) finding algorithm.
if(accuracy < 0.65): #Used 65% accuracy as benchmark for trending year finding algorithm.
for a in range(3,l-4):
X = df.iloc[0,a:l].values
y = test[a:]
regressor = LinearRegression()
regressor.fit(X.reshape(-1,1),y)
accuracy = regressor.score(X.reshape(-1,1),y)
if (accuracy > accuracy_max):
accuracy_max = accuracy
print accuracy_max
trendChangingYear = a
print trendChangingYear #Printing Trend Changing Year on server terminal.
print test[trendChangingYear]
print xTrain[trendChangingYear-2]
year = int(year)
y = test[2:]
b = []
#If accuracy is Lower than 65%, only Visualization of the data is shown - no predictions.
if accuracy < 0.65:
for k in range(2001,2017):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
year = 2016
msg = "Data is not Suitable for prediction"
#Else predictions are shown and Run time data and labels are added to the graph.
else:
for j in range(2017,year+1):
prediction = regressor.predict(j)
if(prediction < 0):
prediction = 0
y = np.append(y,prediction)
y = np.append(y,0)
for k in range(2001,year+1):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
msg = ""
#Finally the template is rendered.
return render_template('ipc.html',data = [accuracy,yTrain,xTrain,state,year,data1,X,y,test,l],msg = msg, state=state, year=year, C_type=C_type,pred_data = y,years = yearLable)
@app.route('/sll.html',methods = ['POST'])
def sll():
year = request.form.get("Predict_Year") #Year fetching From UI.
C_type = request.form.get("C_Type") #Crime type fetching from UI
state = request.form.get("state") #State name fetching from UI
#reading CSV file.
df = pd.read_csv("static/StateSLLPred2001_16.csv", header=None)
data1 = df.loc[df[0]==state].values #Selecting State and its attributes.
for x in data1:
if x[1] == C_type:
test = x
break
l = len(df.columns)
trendChangingYear = 2
accuracy_max = 0.65
# Year array for Javascript for Labeling to Graph
xTrain = np.array([2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016])
yTrain = test[2:18]
X = df.iloc[0,2:l].values
y = test[2:]
regressor = LinearRegression() #regression Algorithm Called.
regressor.fit(X.reshape(-1,1),y) #Data set is fitted in regression and Reshaped it.
accuracy = regressor.score(X.reshape(-1,1),y) #Finding Accuracy of Prdictions.
print accuracy
accuracy_max = 0.65
#Trending year(Influence Year) finding algorithm.
if(accuracy < 0.65): #Used 65% accuracy as benchmark for trending year finding algorithm.
for a in range(3,l-4):
X = df.iloc[0,a:l].values
y = test[a:]
regressor = LinearRegression()
regressor.fit(X.reshape(-1,1),y)
accuracy = regressor.score(X.reshape(-1,1),y)
if (accuracy > accuracy_max):
accuracy_max = accuracy
print accuracy_max
trendChangingYear = a
print trendChangingYear #Printing Trend Changing Year on server terminal.
print test[trendChangingYear]
print xTrain[trendChangingYear-2]
year = int(year)
y = test[2:]
b = []
#If accuracy is Lower than 65%, only Visualization of the data is shown - not predictions.
if accuracy < 0.65:
for k in range(2001,2017):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
year = 2016
msg = "Data is not Suitable for prediction"
#Else predictions are shown and Run time data and labels are added to the graph.
else:
for j in range(2017,year+1):
prediction = regressor.predict(j)
if(prediction < 0):
prediction = 0
y = np.append(y,prediction)
y = np.append(y,0)
for k in range(2001,year+1):
a = str(k)
b = np.append(b,a)
y = list(y)
yearLable = list(b)
msg = ""
#Finally the template is rendered.
return render_template('sll.html',data = [accuracy,yTrain,xTrain,state,year,data1,X,y,test,l],msg = msg, state=state, year=year, C_type=C_type,pred_data = y,years = yearLable)
#routing Path for About page.
@app.route('/About.html')
def About():
return render_template("/About.html")
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000, debug=True)