-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMeans_HackData.py
390 lines (244 loc) · 12.9 KB
/
KMeans_HackData.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
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 6 19:46:27 2020
@author: anuju
"""
# In[1]:
import os
os.environ['JAVA_HOME'] = 'C:/Program Files/Java/jre1.8.0_241'
os.environ['PYSPARK_SUBMIT_ARGS'] = "--master local[2] pyspark-shell"
from pyspark.sql import SparkSession
import pyspark.sql.functions as F
spark = SparkSession.builder.appName('k-means clustering').getOrCreate()
# Loading datafile "hack_data.txt"
dataset = spark.read.csv("c:/users/anuju/desktop/Internet_Of_Things/Assignments/hack_data.txt",header=True,inferSchema=True)
dataset.head()
dataset.show(10)
dataset.describe().show()
#UNDERSTANDING THE DATA
#plotting a histogram of the data
dataset.toPandas().hist(column=dataset.columns, bins=20, figsize=(7,7))
# Correlation Matrix plot
import seaborn as sns
import numpy as np
import matplotlib as plt
# Get Correlations
corr = dataset.toPandas()[dataset.columns].corr()
#plotting the heatmap
sns.heatmap(corr,annot=True)
# In[2]:
# ## Format the Data
#Converting Categorical Data
from pyspark.ml.feature import StringIndexer, OneHotEncoder, VectorAssembler
from pyspark.ml import Pipeline
import pyspark.sql.functions as F
stage_string = StringIndexer().setInputCol("Location").setOutputCol("LocationIndex")
stage_one_hot = OneHotEncoder().setInputCol("LocationIndex").setOutputCol("LocationCoded")
ppl = Pipeline(stages = [stage_string , stage_one_hot])
df = ppl.fit(dataset).transform(dataset)
df.toPandas().to_csv('HackData_afterTransform.csv')
from pyspark.ml.linalg import Vectors
from pyspark.ml.feature import VectorAssembler
print(df.columns)
# Extracting Features without Location
Assembler_NoLocation = VectorAssembler(
inputCols=['Session_Connection_Time',
'Bytes Transferred',
'Kali_Trace_Used',
'Servers_Corrupted',
'Pages_Corrupted',
'WPM_Typing_Speed'],
outputCol="features_NoLocation")
FeatureData_NoLocation= Assembler_NoLocation.transform(df)
FeatureData_NoLocation.toPandas().to_csv('HackDataFeatures_NoLocation.csv')
FeatureData_NoLocation.show(5)
# Extracting Features with Location after String Indexer
Assembler_LocationIndex = VectorAssembler(
inputCols=['Session_Connection_Time',
'Bytes Transferred',
'Kali_Trace_Used',
'Servers_Corrupted',
'Pages_Corrupted',
'WPM_Typing_Speed',
'LocationIndex'],
outputCol="features_LocationIndex")
FeatureData_LocationIndex= Assembler_LocationIndex.transform(df)
FeatureData_LocationIndex.toPandas().to_csv('HackDataFeatures_LocationIndex.csv')
FeatureData_LocationIndex.show(5)
# Extracting Features with Location after StringIndexer and OneHotEncoder
Assembler_LocationCoded = VectorAssembler(
inputCols=['Session_Connection_Time',
'Bytes Transferred',
'Kali_Trace_Used',
'Servers_Corrupted',
'Pages_Corrupted',
'WPM_Typing_Speed',
'LocationCoded'],
outputCol="features_LocationCoded")
FeatureData_LocationCoded= Assembler_LocationCoded.transform(df)
FeatureData_LocationCoded.toPandas().to_csv('HackDataFeatures_LocationCoded.csv')
FeatureData_LocationCoded.show(5)
# In[3]:
#Scaling the data without Location
from pyspark.ml.feature import StandardScaler
scaler_NoLocation = StandardScaler(inputCol="features_NoLocation", outputCol="scaledFeatures_NoLocation", withStd=True, withMean=False)
# Compute summary statistics by fitting the StandardScaler
scalerModel_NoLocation = scaler_NoLocation.fit(FeatureData_NoLocation)
# Normalize each feature to have unit standard deviation.
FinalData_NoLocation = scalerModel_NoLocation.transform(FeatureData_NoLocation)
FinalData_NoLocation.toPandas().to_csv('HackDataFinal_NoLocation.csv')
###################################################################################################################
#Scaling the data with Location after String Indexer
scaler_LocationIndex = StandardScaler(inputCol="features_LocationIndex", outputCol="scaledFeatures_LocationIndex", withStd=True, withMean=False)
# Compute summary statistics by fitting the StandardScaler
scalerModel_LocationIndex = scaler_LocationIndex.fit(FeatureData_LocationIndex)
# Normalize each feature to have unit standard deviation.
FinalData_LocationIndex = scalerModel_LocationIndex.transform(FeatureData_LocationIndex)
FinalData_LocationIndex.toPandas().to_csv('HackDataFinal_LocationIndex.csv')
##################################################################################################################
#Scaling the data with Location after OneHotEncoder
scaler_LocationCoded = StandardScaler(inputCol="features_LocationCoded", outputCol="scaledFeatures_LocationCoded", withStd=True, withMean=False)
# Compute summary statistics by fitting the StandardScaler
scalerModel_LocationCoded = scaler_LocationCoded.fit(FeatureData_LocationCoded)
# Normalize each feature to have unit standard deviation.
FinalData_LocationCoded = scalerModel_LocationCoded.transform(FeatureData_LocationCoded)
FinalData_LocationCoded.toPandas().to_csv('HackDataFinal_LocationCoded.csv')
# In[4]:
# ## Train the Model and Evaluate
from pyspark.ml.clustering import KMeans
#Importing matplotlib to plot a graph
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import math
# Finding the Appropriate K Value by Elbow Method
# Without Location
wssse= []
KMin = 2
KMax = 34
KStep = 1
index = 0
for iLoop in range(KMin,KMax,KStep):
kmeans = KMeans(featuresCol='scaledFeatures_NoLocation', k=iLoop)
model = kmeans.fit(FinalData_NoLocation)
wssse.append(model.computeCost(FinalData_NoLocation))
print("When k=" + str(iLoop) +" Within Set Sum of Squared Errors = " + str(wssse[index]))
index += 1
fig, ax = plt.subplots(1,1, figsize =(8,8))
ax.plot(range(KMin,KMax, KStep),wssse[0:math.ceil(KMax/KStep)],'--bo')
ax.xaxis.set_major_locator(ticker.MultipleLocator(KStep))
ax.set_xlabel('k-Value',fontsize = 20)
ax.set_ylabel('WSSSE',fontsize = 20)
ax.grid()
ax.set_title("The Elbow Method to find the k-value (NoLocation)",fontsize = 24)
# With LocationIndex
wssse= []
KMin = 2
KMax = 30
KStep = 1
index = 0
for iLoop in range(KMin,KMax,KStep):
kmeans = KMeans(featuresCol='scaledFeatures_LocationIndex', k=iLoop)
model = kmeans.fit(FinalData_LocationIndex)
wssse.append(model.computeCost(FinalData_LocationIndex))
print("When k=" + str(iLoop) +" Within Set Sum of Squared Errors = " + str(wssse[index]))
index += 1
fig, ax = plt.subplots(1,1, figsize =(8,8))
ax.plot(range(KMin,KMax, KStep),wssse[0:math.ceil(KMax/KStep)],'--bo')
ax.xaxis.set_major_locator(ticker.MultipleLocator(KStep))
ax.set_xlabel('k-Value',fontsize = 20)
ax.set_ylabel('WSSSE',fontsize = 20)
ax.grid()
ax.set_title("The Elbow Method to find the k-value (With LocationIndex)",fontsize = 24)
# With LocationCoded
wssse= []
KMin = 2
KMax = 300
KStep = 10
index = 0
for iLoop in range(KMin,KMax,KStep):
kmeans = KMeans(featuresCol='scaledFeatures_LocationCoded', k=iLoop)
model = kmeans.fit(FinalData_LocationCoded)
wssse.append(model.computeCost(FinalData_LocationCoded))
print("When k=" + str(iLoop) +" Within Set Sum of Squared Errors = " + str(wssse[index]))
index += 1
fig, ax = plt.subplots(1,1, figsize =(8,8))
ax.plot(range(KMin,KMax, KStep),wssse[0:math.ceil(KMax/KStep)],'--bo')
ax.xaxis.set_major_locator(ticker.MultipleLocator(KStep))
ax.set_xlabel('k-Value',fontsize = 20)
ax.set_ylabel('WSSSE',fontsize = 20)
ax.grid()
ax.set_title("The Elbow Method to find the k-value (With LocationCoded)",fontsize = 24)
#######################################################################################################################################
#Training the K-Means Model
#Without Location
Final_KMeans_NoLocation = KMeans(featuresCol='scaledFeatures_NoLocation', k=6)
Final_Model_NoLocation = Final_KMeans_NoLocation.fit(FinalData_NoLocation)
#Evaluation of Model
FinalModelWSSSE_NoLocation = Final_Model_NoLocation.computeCost(FinalData_NoLocation)
print ("Within Set Sum of Suared Errors = " + str(FinalModelWSSSE_NoLocation))
#With LocationIndex
Final_KMeans_LocationIndex = KMeans(featuresCol='scaledFeatures_LocationIndex', k=9)
Final_Model_LocationIndex = Final_KMeans_LocationIndex.fit(FinalData_LocationIndex)
#Evaluation of Model
FinalModelWSSSE_LocationIndex = Final_Model_LocationIndex.computeCost(FinalData_LocationIndex)
print ("Within Set Sum of Suared Errors = " + str(FinalModelWSSSE_LocationIndex))
#With LocationCoded
Final_KMeans_LocationCoded = KMeans(featuresCol='scaledFeatures_LocationCoded', k=192)
Final_Model_LocationCoded = Final_KMeans_LocationCoded.fit(FinalData_LocationCoded)
#Evaluation of Model
FinalModelWSSSE_LocationCoded = Final_Model_LocationCoded.computeCost(FinalData_LocationCoded)
print ("Within Set Sum of Suared Errors = " + str(FinalModelWSSSE_LocationCoded))
# In[5]:
# Shows the result.
#Without Location
centers = Final_Model_NoLocation.clusterCenters()
print("Cluster Centers: ")
for center in centers:
print(center)
# Predict the label of each hacking attempt
Final_Model_NoLocation.transform(FinalData_NoLocation).select('prediction').show(10)
#With LocationIndex
centers = Final_Model_LocationIndex.clusterCenters()
print("Cluster Centers: ")
for center in centers:
print(center)
# Predict the label of each hacking attempt
Final_Model_LocationIndex.transform(FinalData_LocationIndex).select('prediction').show(10)
#With LocationCoded
centers = Final_Model_LocationCoded.clusterCenters()
print("Cluster Centers: ")
for center in centers:
print(center)
# Predict the label of each hacking attempt
Final_Model_LocationCoded.transform(FinalData_LocationCoded).select('prediction').show(10)
# In[6]:
#formingClusters
#Without Location
clusters_NoLocation = Final_Model_NoLocation.transform(FinalData_NoLocation).select('*')
clusters_NoLocation.groupBy("prediction").count().orderBy(F.desc("count")).show()
clusters_NoLocation.show()
clusters_NoLocation_pd = clusters_NoLocation.toPandas()
clusters_NoLocation_pd.to_csv("Clusters_NoLocation.csv")
#With LocationIndex
clusters_LocationIndex = Final_Model_LocationIndex.transform(FinalData_LocationIndex).select('*')
clusters_LocationIndex.groupBy("prediction").count().orderBy(F.desc("count")).show()
clusters_LocationIndex.show()
clusters_LocationIndex_pd = clusters_LocationIndex.toPandas()
clusters_LocationIndex_pd.to_csv("clusters_LocationIndex.csv")
#With LocationCoded
clusters_LocationCoded = Final_Model_LocationCoded.transform(FinalData_LocationCoded).select('*')
clusters_LocationCoded.groupBy("prediction").count().orderBy(F.desc("count")).show()
clusters_LocationCoded.show()
clusters_LocationCoded_pd = clusters_LocationCoded.toPandas()
clusters_LocationCoded_pd.to_csv("clusters_LocationCoded.csv")
# Plotting Clusters
# Pairwise Scatterplot
dataScatterPlot_NoLocation = clusters_NoLocation_pd[['Session_Connection_Time', 'Bytes Transferred', 'Kali_Trace_Used','Servers_Corrupted', 'Pages_Corrupted','WPM_Typing_Speed','prediction']]
Variables_NoLocation = clusters_NoLocation_pd[['Session_Connection_Time', 'Bytes Transferred', 'Kali_Trace_Used','Servers_Corrupted', 'Pages_Corrupted','WPM_Typing_Speed']]
g_NoLocation = sns.pairplot( dataScatterPlot_NoLocation, vars = Variables_NoLocation, hue = "prediction", diag_kind = 'kde',palette= "husl", plot_kws = {'alpha': 0.6, 's': 80, 'edgecolor': 'k'})
dataScatterPlot_LocationIndex = clusters_LocationIndex_pd[['Session_Connection_Time', 'Bytes Transferred', 'Kali_Trace_Used','Servers_Corrupted', 'Pages_Corrupted','WPM_Typing_Speed','LocationIndex','prediction']]
Variables_LocationIndex = clusters_LocationIndex_pd[['Session_Connection_Time', 'Bytes Transferred', 'Kali_Trace_Used','Servers_Corrupted', 'Pages_Corrupted','WPM_Typing_Speed','LocationIndex']]
g_LocationIndex = sns.pairplot( dataScatterPlot_LocationIndex, vars = Variables_LocationIndex, hue = "prediction", diag_kind = 'kde',palette= "husl", plot_kws = {'alpha': 0.6, 's': 80, 'edgecolor': 'k'})
dataScatterPlot_LocationCoded = clusters_LocationCoded_pd[['Session_Connection_Time', 'Bytes Transferred', 'Kali_Trace_Used','Servers_Corrupted', 'Pages_Corrupted','WPM_Typing_Speed','prediction']]
Variables_LocationCoded = clusters_LocationCoded_pd[['Session_Connection_Time', 'Bytes Transferred', 'Kali_Trace_Used','Servers_Corrupted', 'Pages_Corrupted','WPM_Typing_Speed']]
g_LocationCoded = sns.pairplot( dataScatterPlot_LocationCoded, vars = Variables_LocationCoded, hue = "prediction", diag_kind = 'kde',palette= "husl", plot_kws = {'alpha': 0.6, 's': 80, 'edgecolor': 'k'})