-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalCode.py
623 lines (376 loc) · 13.6 KB
/
finalCode.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
##Exploring Traveler data
import pandas as pd
import matplotlib.pyplot as plt
#get_ipython().run_line_magic('pylab', 'inline')
train_file="deaths-in-india-satp-dfe_Final.csv"
data = pd.read_csv(train_file, header = 0,index_col=None)
# ## Splitting Data into train data and test data
# In[2]:
df_train = data.head(22000)
df_test = data.tail(5000)
df_test.drop('verb',axis = 1, inplace = True)
# ## Conacatenate train and test data for Preprocessing
# In[3]:
data = pd.concat((df_train, df_test), axis=0, ignore_index=True)
# ## Preprocessing
# In[4]:
#dropping _goden as this attribute contains same value for all of its records
data.drop('_golden', axis = 1, inplace = True)
print("Dropping _golden")
#dropping _unit_state as this attribute contains same value for all of its records
data.drop('_unit_state',axis = 1, inplace = True)
print("Dropping _unit_state")
#dropping _trusted_judgments as this attribute contains same value for all of its records
data.drop('_trusted_judgments',axis = 1, inplace = True)
print("Dropping _trusted_judgments")
#dropping _last_judgment_at as this does not help for our prediction
data.drop('_last_judgment_at',axis = 1, inplace = True)
print("Dropping _last_judgment_at")
#dropping _accuracy as this attribute contains same value for all of its records
data.drop('accuracy',axis = 1, inplace = True)
print("Dropping accuracy")
#dropping canddist as this attribute does not help for our prediction
data.drop('canddist',axis = 1, inplace = True)
print("Dropping canddist")
#dropping civilians_gold as this attribute contains same value for all of its records
data.drop('civilians_gold',axis = 1, inplace = True)
print("Dropping civilians_gold")
#dropping date as this attribute does not help for our prediction
data.drop('date',axis = 1, inplace = True)
print("Dropping date")
#dropping militants_terrorists_insurgents_gold as this attribute does not contain any value for all of its records
data.drop('militants_terrorists_insurgents_gold',axis = 1, inplace = True)
print("Dropping militants_terrorists_insurgents_gold")
#droppin object as it is a redundant attribute
data.drop('object',axis = 1, inplace = True)
print("Dropping object")
#dropping objectcleanpp as this attribute does nothelp for our prediction
data.drop('objectcleanpp',axis = 1, inplace = True)
print("Dropping objectcleanpp")
#droppin object as it is a redundant attribute
data.drop('objectcount',axis = 1, inplace = True)
print("Dropping objectcount")
#droppin object as does not help for our prediction
data.drop('pid',axis = 1, inplace = True)
print("Dropping pid")
#droppin object as does not help for our prediction
data.drop('rid',axis = 1, inplace = True)
print("Dropping rid")
#dropping security_forces_gold as this attribute does not contain any value for all of its records
data.drop('security_forces_gold',axis = 1, inplace = True)
print("Dropping security_forces_gold")
#dropping objectcleanpp as this attribute does nothelp for our prediction
data.drop('sentence',axis = 1, inplace = True)
print("Dropping sentence")
#dropping sid as this attribute is not helpful possible prediction of future attacks
data.drop('sid',axis = 1, inplace = True)
print("Dropping sid")
#dropping srid as this attribute does not contain any value for all of its records
data.drop('srid',axis = 1, inplace = True)
print("Dropping srid")
#droppin state as it is a redundant attribute
data.drop('state',axis = 1, inplace = True)
print("Dropping state")
#droppin state as it is not helpful possible prediction of future attacks
data.drop('subject',axis = 1, inplace = True)
print("Dropping subject")
#dropping svmlabel2 as this attribute is not helpful possible prediction of future attacks
data.drop('svmlabel2',axis = 1, inplace = True)
print("Dropping svmlabel2")
#dropping svmlabel2prob as this attribute is not helpful possible prediction of future attacks
data.drop('svmlabel2prob',axis = 1, inplace = True)
print("Dropping svmlabel2prob")
#dropping svmobjecttypelab as this attribute contains same value for all of its records
data.drop('svmobjecttypelab',axis = 1, inplace = True)
print("Dropping svmobjecttypelab")
#dropping svmobjecttypeprob as this attribute is not helpful possible prediction of future attacks
data.drop('svmobjecttypeprob',axis = 1, inplace = True)
print("Dropping svmobjecttypeprob")
#dropping total_number_of_people_gold as this attribute does not contain any value for all of its records
data.drop('total_number_of_people_gold',axis = 1, inplace = True)
print("Dropping total_number_of_people_gold")
# ### Computing NULL values in all attributes
# In[5]:
# Compute Null percentage of each feature.
df_all_null = (data.isnull().sum() / data.shape[0]) * 100
#df_all_null = (df_all.isnull().sum())
df_all_null[df_all_null > 0]
# In[6]:
df_all_null.plot(kind="bar",color="red",rot=80)
# ## Filling missing values
# In[7]:
data.to_csv("preprocessed.csv")
# In[8]:
# Fill state_full column
print("Filling state_full column...")
data['state_full'].fillna('NA', inplace=False)
print("Filling state_full column...completed")
data = data.dropna(axis = 0, how ='any')
# # Computing null after dropping
# In[9]:
# Compute Null percentage of each feature.
df_all_null = (data.isnull().sum() / data.shape[0]) * 100
#df_all_null = (df_all.isnull().sum())
df_all_null[df_all_null > 0]
df_all_null.plot(kind="bar",color="red",rot=80)
# ## Data Transformation
# In[10]:
data['report_date'] = pd.to_datetime(data.report_date)
#converting into Standard date format Y-M-D
data['report_date'] = pd.to_datetime(data['report_date'], format='%Y-%m-%d')
# In[11]:
data['report_date']
# ## Data addition
# In[12]:
#adding new columns
data['attack_month'] = data['report_date'].dt.month
data['attack_day'] = data['report_date'].dt.day
# In[13]:
data.drop('report_date',axis = 1, inplace = True)
# ## Data transformation
# In[14]:
#replacing similar data of the attribute
replaceVerb=data['verb']
import re
for var in replaceVerb:
if(re.search("killing",str(var)) or re.search("kill",str(var))):
var2='killed'
data['verb'].replace(var,var2,inplace=True)
elif(re.search("arresting",str(var)) or re.search("arrest",str(var))):
var2='arrested'
data['verb'].replace(var,var2,inplace=True)
elif(re.search("surrender",str(var)) or re.search("surrendering",str(var))):
var2='surrendered'
data['verb'].replace(var,var2,inplace=True)
elif(re.search("injuring",str(var)) or re.search("injure",str(var))):
var2='injured'
data['verb'].replace(var,var2,inplace=True)
# ## One Hot Encoding
# In[15]:
def convert_to_binary(df, column_to_convert):
categories = list(df[column_to_convert].drop_duplicates())
for category in categories:
cat_name = str(category).replace(" ", "_").replace("(", "").replace(")", "").replace("/", "_").replace("-", "").lower()
col_name = column_to_convert[:4] + '_' + cat_name[:10]
df[col_name] = 0
df.loc[(df[column_to_convert] == category), col_name] = 1
return df
# One Hot Encoding
print("One Hot Encoding categorical data...")
columns_to_convert = ['state_full','svmlabel1','districtmatch']
for column in columns_to_convert:
data = convert_to_binary(data,column)
data.drop(column, axis=1, inplace=True)
print("One Hot Encoding categorical data...completed")
# In[16]:
#to find total number of deaths based on profession
security=data['svml_security'].sum()
terrorist=data['svml_terrorist'].sum()
civilian=data['svml_civilian'].sum()
public=data['svml_public'].sum()
private=data['svml_private'].sum()
# In[17]:
# Pie chart
labels = 'security','public', 'terrorist', 'civilian' ,'private'
sizes = [security,public,terrorist,civilian,private]
explode = (0, 0, 0.2, 0.1, 0)
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode =explode ,labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal')
plt.show()
# In[18]:
#from above pie cart we can say that major part of killed were terrorists
# ## Save Preprocessed Data to csv file
# In[19]:
file4 = 'ppppreprocessed.csv'
data.to_csv(file4)
# In[20]:
#create data frame for classification
df_all = pd.DataFrame(data=data)
# ## Split preprocessed data into train data and test data
# In[21]:
df_train = df_all.head(22000)
df_test = df_all.tail(5000)
df_all1 = df_all
# In[22]:
#dropping column which we want to predivt from test data
df_test.drop('verb',axis = 1, inplace = True)
# In[23]:
df_all.drop('verb',axis = 1, inplace = True)
# In[24]:
#temporary variables
df_train1 = df_train
df_test1 = df_test
df_all1 = df_all
# In[25]:
df_train1.columns
# In[26]:
df_train['verb'].head()
# In[27]:
df_test1.columns
# In[28]:
df_all.columns
# In[29]:
#seeting _unit_id as a index of dataframe
df_train1.set_index('_unit_id', inplace=True)
# ## Label Encoding for Categorical data
# In[30]:
from sklearn.preprocessing import LabelEncoder
id_train = df_train1.index.values
labels = df_train1['verb']
# Label encoding for the categorical data eg: ...NDF -> 7, US -> 10...
le = LabelEncoder()
y = le.fit_transform(labels)
X = df_train1.drop('verb', axis=1, inplace=False)
# In[31]:
X.shape
# In[ ]:
X.head()
# In[ ]:
from sklearn.model_selection import train_test_split
## Spliting of training dataset into 70% training data and 30% testing data randomly
features_train, features_test, labels_train, labels_test = train_test_split(X, y, test_size=0.3, random_state=42)
# # Gradient Boosting
# In[69]:
'''
## Decision Tree
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# ## Decission tree Classifier
# In[65]:
## Decision Tree
from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# ## Gaussian Naive Bayes Classifier
# In[66]:
## Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# ## SVM Classifier
# In[67]:
## SVM
from sklearn import svm
clf = svm.SVC(kernel="rbf")
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# # RandomForest classifier
# In[68]:
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# # Logistic Regression
# In[70]:
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# # K Nearest Neighbours
# In[72]:
from sklearn.neighbors import KNeighborsClassifier
clf = KNeighborsClassifier()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# # Bagging Classifier
# In[71]:
from sklearn.ensemble import BaggingClassifier
clf = BaggingClassifier()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# # Extra Trees Classifier
# In[73]:
from sklearn.ensemble import ExtraTreesClassifier
clf = ExtraTreesClassifier()
clf.fit(features_train, labels_train)
prediction = clf.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
'''
# # Approach 2
# ## All At once
# In[ ]:
from sklearn.ensemble import *
from sklearn.neighbors import *
from sklearn.svm import *
from sklearn.dummy import *
from sklearn.tree import *
from sklearn.gaussian_process import *
ensembles = [
AdaBoostClassifier(),
BaggingClassifier(),
ExtraTreesClassifier(),
GradientBoostingClassifier(),
IsolationForest(),
RandomForestClassifier()
]
neighbors = [
KNeighborsClassifier(),
RadiusNeighborsClassifier(),
NearestCentroid()
]
svms = [
LinearSVC(),
NuSVC(),
SVC(),
]
trees = [
DecisionTreeClassifier(),
ExtraTreeClassifier(),
]
extraa = [
DummyClassifier(),
GaussianProcessClassifier()
]
models = []
models.extend(ensembles)
models.extend(neighbors)
models.extend(svms)
models.extend(trees)
models.extend(extraa)
for model in models:
try:
print("---------------------------------------------------------------")
print(str(model))
print("---------------------------------------------------------------")
file = open("Model_output_logger.txt", "a")
model.fit(features_train, labels_train)
prediction = model.predict(features_test)
## Computing accuracy
accuracy = accuracy_score(labels_test , prediction)
print("Accuracy: %.2f%%\n\n\n\n\n\n" % (accuracy * 100.0))
file.write("model : " + str(model) + "accuracy : " + str(accuracy * 100.0) + "\n\n")
file.close()
except:
print("\n\nerror in")
print(model)
# In[ ]: