-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
525 lines (281 loc) · 10 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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import seaborn as sns
# In[2]:
# Loading the data
df = pd.read_csv('dataset.csv')
df
# # 1. Exploratory Data Analysis (EDA)
# In[3]:
df = df.drop(['Latitude', 'Longitude'], axis = 1) #redundant feature, already used in location
# In[4]:
# converting dates to pandas datetime format
df.Date = pd.to_datetime(df.Date, format='%m/%d/%Y %I:%M:%S %p')
# setting the index to be the date
df.index = pd.DatetimeIndex(df.Date)
df
# In[5]:
# Plot: Heat Map
correlation = df.corr()
sns.set(rc = {'figure.figsize':(15,8)})
sns.heatmap(correlation, xticklabels = correlation.columns, yticklabels = correlation.columns, annot = True)
# In[6]:
# Plot: Number of crimes per month
plt.figure(figsize=(11,5))
df.resample('M').size().plot(legend=False)
plt.title('Number of crimes per month')
plt.xlabel('Months')
plt.ylabel('Number of crimes')
plt.show()
# In[7]:
# Plot: Number of crimes by type
plt.figure(figsize=(8,10))
df.groupby([df['Primary Type']]).size().sort_values(ascending=True).plot(kind='barh')
plt.title('Number of crimes by type')
plt.ylabel('Crime Type')
plt.xlabel('Number of crimes')
plt.show()
# In[8]:
# Plot: Primary Types Vs. Year
df_count_date = df.pivot_table('ID', aggfunc=np.size, columns='Primary Type', index=df.index.date, fill_value=0)
df_count_date.index = pd.DatetimeIndex(df_count_date.index)
plot = df_count_date.rolling(365).sum().plot(figsize=(30, 30), subplots=True, layout=(-1, 3), sharex=False, sharey=False)
# In[9]:
# Plot: Crimes Occuring by the days
days = ['Monday','Tuesday','Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
df.groupby([df.index.dayofweek]).size().plot(kind='barh')
plt.ylabel('Days of the week')
plt.yticks(np.arange(7), days)
plt.xlabel('Number of crimes')
plt.title('Number of crimes by days')
plt.show()
# In[10]:
# Plot: Crimes Occuring by Months
df.groupby([df.index.month]).size().plot(kind='barh')
plt.ylabel('Months of the year')
plt.xlabel('Number of crimes')
plt.title('Number of crimes by months')
plt.show()
# In[11]:
# Plot: Crimes Occuring by Location
plt.figure(figsize=(10, 30))
df.groupby([df['Location Description']]).size().sort_values(ascending=True).plot(kind='barh')
plt.title('Number of crimes by Location')
plt.ylabel('Crime Location')
plt.xlabel('Number of crimes')
plt.show()
# # 2. Preprocessing Data
# In[12]:
# resetting the index
df.reset_index(drop = True)
# In[13]:
df.info()
# In[14]:
df = df.astype({"Unnamed: 0": object})
df = df.astype({"ID": object})
df = df.astype({"Beat": object})
df = df.astype({"District": object})
df = df.astype({"Ward": object})
df = df.astype({"Community Area": object})
df = df.astype({"Year": object})
df = df.astype({"X Coordinate": object})
df = df.astype({"Y Coordinate": object})
df.info()
# In[15]:
df.isnull().sum()
# In[16]:
df.nunique()
# # 3.1. Feature Selection
# ## 3.1.1 Entropy
# In[17]:
def entropy(y):
probs = [] # Probabilities of each class label
for c in set(y): # Set gets a unique set of values. We're iterating over each value
num_same_class = sum(y == c) # Remember that true == 1, so we can sum.
p = num_same_class / len(y) # Probability of this class label
probs.append(p)
return sum(-p * np.log2(p) for p in probs)
# In[18]:
columns = ['Unnamed: 0', 'ID', 'Case Number', 'Date', 'Block', 'IUCR',
'Primary Type', 'Description', 'Location Description', 'Arrest',
'Domestic', 'Beat', 'District', 'Ward', 'Community Area', 'FBI Code',
'X Coordinate', 'Y Coordinate', 'Year', 'Updated On', 'Location']
"""This code takes too long to execute. So, the result has been appended as a column "Entropy" in df_ent
#Code:
for column in columns:
print(entropy(df[column]))
df_ent.append(entropy(df[column]))
"""
# In[19]:
df_ent = pd.DataFrame()
df_ent["Features"] = columns
df_ent["Entropy"] = [16.60964047441802, 16.60964047441802, 16.60964047441802, 16.50325437354276,
13.842338152132987, 5.6494058088293935, 3.482725829513007, 5.518046958190912,4.0556159613837135,
0.839130421235637,0.5773624295265078,8.0915039193288,4.403738721836829,5.475453895970417,
5.826020205215316,3.5574293236120447,14.760131667723728,15.11747552923421,4.239196753831807,
2.8553923756999566,15.74133241017893]
df_ent
# In[20]:
#Dropping high entropy features(Entropy > 10)
df_drop = df.drop(columns =['Unnamed: 0', 'ID', 'Case Number', 'Date',
'Block', 'X Coordinate', 'Y Coordinate', 'Location'])
df_drop.reset_index(drop = True)
# # Part 1 - 100,000 Rows
# ## 4. Unsupervised Learning
# ### 4.1. K Modes Clustering
# In[21]:
from kmodes.kmodes import KModes
# In[22]:
#Elbow Plot
""""
cost =[]
K = range(0,31)
for num_clusters in list(K):
kmode = KModes(n_clusters = num_clusters, init = "random", n_init = 2, verbose = 1)
kmode.fit_predict(df1)
cost.append(kmode.cost_)
plt.plot(K, cost, 'bx-')
plt.xlabel('No. of clusters')
plt.ylabel('Cost')
plt.title('Elbow Method For Optimal k')
plt.show()
"""
# In[23]:
kmodes = KModes(n_clusters = 31, init = "random", n_init = 5, verbose = 1) #given primary type as the class label which 31
kmodes_clusters = kmodes.fit_predict(df_drop)
kmodes_clusters
# In[24]:
set(kmodes_clusters)
# In[25]:
print(kmodes_clusters)
# In[26]:
data_kmodes= pd.read_csv('dataset.csv')
data_kmodes.insert(0, "KMode_Cluster", kmodes_clusters, True)
data_kmodes
# ### 4.2. Hierarchial Clustering
# In[27]:
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from scipy.cluster import hierarchy
#Vectorizing
X = CountVectorizer().fit_transform(df_drop)
X = TfidfTransformer().fit_transform(X)
#Clustering
X = X.todense()
threshold = 0.1
Z = hierarchy.linkage(X,"average", metric="cosine")
hierarchial_clusters = hierarchy.fcluster(Z, threshold, criterion="distance")
hierarchial_clusters
# In[28]:
set(hierarchial_clusters)
# In[29]:
print(hierarchial_clusters)
# In[30]:
#data_hierarchial= pd.read_csv('dataset.csv')
#data_hierarchial.insert(0, "Hierarchial_Cluster", hierarchial_clusters, True)
#data_hierarchial
# # Part 2 - 25,000 Rows
# We are considering 25k data points due to technical limitations
# ## 3.2. Feature Engineering
# In[31]:
#These data points have unique primary crime type, later we will need to drop it for splitting
df_drop.drop([df_drop.index[64744], df_drop.index[94033]], axis=0, inplace=True)
# ### 3.2.1. Splitting Data
# In[32]:
# splitting data into 25k
from sklearn.model_selection import train_test_split
X = df_drop[['IUCR','Description','Location Description','Arrest','Domestic','Beat','District',
'Ward','Community Area','FBI Code','Year','Updated On']]
Y = df_drop['Primary Type']
data25k, data75k, test25k, test75k= train_test_split(X, Y, test_size = 0.75, random_state = 50)
data25 = pd.concat([data25k, test25k], axis=1)
data25 = data25.reset_index(drop= True)
data25.to_csv("data25.csv")
# ### 3.2.2. One Hot Encoding
# In[33]:
column_names = []
for row in df_drop:
column_names.append(row)
column_names
# In[34]:
# generate binary values using get_dummies
df_dum = pd.get_dummies(data25, columns = column_names)
df_dum = df_dum.reset_index(drop = True)
# ## 4. Unsupervised Learning
# ### 4.3. Spectral Clustering
# In[35]:
# finding cosine similarity maxtrix to be passed as a argument to spectral clustering algorithm
from sklearn.metrics.pairwise import cosine_similarity
cosine = cosine_similarity(df_dum, df_dum)
#Takes too long to execute
#print(cosine)
# In[36]:
from sklearn.cluster import SpectralClustering
# Building the clustering model
spectral_model = SpectralClustering(eigen_solver=None, n_components=None,
random_state=None, n_init=10, gamma=1.0, affinity='precomputed',
n_neighbors=10, eigen_tol=0.0, assign_labels='kmeans', degree=3,
coef0=1, kernel_params=None, n_jobs=3, verbose=False)
# Training the model and Storing the predicted cluster labels
spectral_clusters = spectral_model.fit_predict(cosine)
# In[37]:
set(spectral_clusters)
# In[38]:
print(spectral_clusters)
# In[39]:
data_spectral= pd.read_csv('data25.csv')
data_spectral.insert(0, "Spectral_Cluster", spectral_clusters, True)
data_spectral
# # 5. Visualising Results
# ## 5.1. K Modes
# ### 5.1.1. NMI
# In[40]:
from sklearn.metrics.cluster import normalized_mutual_info_score
NMI_KModes= pd.DataFrame()
Feature = []
NMI_Score = []
for feature in data25.columns:
score = normalized_mutual_info_score(data_kmodes[feature], kmodes_clusters)
Feature.append(feature)
NMI_Score.append(score)
NMI_KModes["Feature"] = Feature
NMI_KModes["NMI_Score"] = NMI_Score
NMI_KModes
# ### 5.1.2. Plot
# In[41]:
# Plot btw KModes Clusters VS Relevant Features
high_NMI = ['IUCR', 'Description','FBI Code','Primary Type' ]
for cluster in set(kmodes_clusters):
print(f'Plot for Cluster:', cluster)
cls = data_kmodes[cluster == kmodes_clusters]
for feature in high_NMI:
cls[feature].value_counts().plot(kind='pie')
plt.show()
# ## 5.2. Spectral Clustering
# ### 5.2.1. NMI
# In[42]:
from sklearn.metrics.cluster import normalized_mutual_info_score
NMI_Spectral= pd.DataFrame()
Feature = []
NMI_Score = []
for feature in data25.columns:
score = normalized_mutual_info_score(data25[feature], spectral_clusters)
Feature.append(feature)
NMI_Score.append(score)
NMI_Spectral["Feature"] = Feature
NMI_Spectral["NMI_Score"] = NMI_Score
NMI_Spectral
# ### 5.2.2. Plot
# In[43]:
# Plot btw Spectral Clusters VS Relevant Features
high_NMI = ['IUCR', 'Description','FBI Code','Primary Type' ]
for cluster in set(spectral_clusters):
print(f'Plot for Cluster:', cluster)
cls = data25[cluster == spectral_clusters]
for feature in high_NMI:
cls[feature].value_counts().plot(kind='pie')
plt.show()