-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_training.py
357 lines (293 loc) · 12.8 KB
/
model_training.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
from time import perf_counter
from pyspark.ml import Pipeline
from pyspark.ml.evaluation import Evaluator
from pyspark.sql.types import *
from pyspark.sql.functions import col, abs
import pyspark.sql.functions as F
from pyspark.sql.window import Window as W
from pyspark.ml.feature import *
from pyspark.ml.linalg import Vectors, VectorUDT
import time
from pyspark.sql import SparkSession
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.ml.classification import RandomForestClassifier, DecisionTreeClassifier, NaiveBayes, LinearSVC
from pyspark.ml.tuning import ParamGridBuilder, CrossValidator
from pyspark.ml.classification import RandomForestClassificationModel
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
start = perf_counter() # to record start time
# Create a Spark session
spark = SparkSession.builder.master("local").appName("PhishingAnalysis") \
.config('spark.ui.port', '4051').getOrCreate()
#Setting Log Level
sc = spark.sparkContext
sc.setLogLevel("FATAL") # Options: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE, WARN
# Load the CSV file from HDFS
df = spark.read.format("csv").option("header", "true").option("inferSchema", "true").load("hdfs://localhost:9000/phishing/data/phishing_urls.csv")
# Defining Feature and Target Columns for Model Training
feature_columns = [
"URLSimilarityIndex",
"LineOfCode",
"NoOfExternalRef",
"NoOfSelfRef",
"NoOfCSS",
"NoOfImage",
"HasSocialNet",
"HasCopyrightInfo",
"HasDescription",
"NoOfJS"
]
# Define target column
target_column = 'label'
# Assembling Features and Transforming Dataset
assembler = VectorAssembler(inputCols=feature_columns, outputCol='features')
# Transform the dataset
df = assembler.transform(df)
# View the dataset with features and target
df.select('features', target_column).show(5)
# Splitting the Data into Training and Testing Sets
# Splitting the data into 80% training and 20% testing
train_data, test_data = df.randomSplit([0.8, 0.2], seed=1947)
# Print the number of records in each set
print(f"Training Data Count: {train_data.count()}")
print(f"Testing Data Count: {test_data.count()}")
# Label Distribution in Training and Testing Sets
# distribution of the label in the training set
train_data.groupBy("label").count().show()
# distribution of the label in the testing set
test_data.groupBy("label").count().show()
# Defining Evaluation Metric for Classification Model
evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
# Defining Random Forest Classifier Model
rf = RandomForestClassifier(labelCol="label", featuresCol="features")
# Defining Hyperparameter Grid for Random Forest
paramGrid = ParamGridBuilder().addGrid(rf.numTrees, [50, 100]) \
.addGrid(rf.maxDepth, [5, 10]) \
.build()
# Setting Up Cross-Validation for Random Forest
rf_cv = CrossValidator(estimator=rf, estimatorParamMaps=paramGrid, evaluator=evaluator, numFolds=3)
# Training, Testing, and Evaluating the Random Forest Model
# Record start time for training
train_start_time = time.time()
# Train model
rf_cv_model = rf_cv.fit(train_data)
# Record end time for training
train_end_time = time.time()
train_duration = train_end_time - train_start_time
print(f"Training Duration: {train_duration:.2f} seconds")
# Record start time for testing
test_start_time = time.time()
# Test model
rf_predictions = rf_cv_model.bestModel.transform(test_data)
rf_accuracy = evaluator.evaluate(rf_predictions)
# Record end time for testing
test_end_time = time.time()
test_duration = test_end_time - test_start_time
print(f"Testing Duration: {test_duration:.2f} seconds")
# Output results
print(f"Random Forest Accuracy: {rf_accuracy:.4f}")
print(f"Best Hyperparameters: ")
best_params = rf_cv_model.bestModel.extractParamMap()
for param, value in best_params.items():
print(f"{param.name}: {value}")
# Defining Decision Tree Model
dt = DecisionTreeClassifier(labelCol="label", featuresCol="features")
# Defining Hyperparameter Grid for Decision Tree Model
paramGrid = ParamGridBuilder().addGrid(dt.maxDepth, [5, 10]) \
.addGrid(dt.minInstancesPerNode, [1, 5]) \
.build()
# Setting Up Cross-Validation for Decision Tree Model
dt_cv = CrossValidator(estimator=dt, estimatorParamMaps=paramGrid, evaluator=evaluator, numFolds=3)
# Decision Tree Model Trainin , Testing & Evaluating
# Start time for training
start_train_time = time.time()
# Train model
dt_cv_model = dt_cv.fit(train_data)
# End time for training
end_train_time = time.time()
train_duration = end_train_time - start_train_time
print(f"Training Duration: {train_duration:.2f} seconds")
# Start time for testing
start_test_time = time.time()
# Test model
dt_predictions = dt_cv_model.bestModel.transform(test_data)
dt_accuracy = evaluator.evaluate(dt_predictions)
# End time for testing
end_test_time = time.time()
test_duration = end_test_time - start_test_time
print(f"Testing Duration: {test_duration:.2f} seconds")
# Output result
print(f"Decision Tree Accuracy: {dt_accuracy:.4f}")
print(f"Best Decision Tree Hyperparameters: ")
best_params = dt_cv_model.bestModel.extractParamMap()
for param, value in best_params.items():
print(f"{param.name}: {value}")
# Naive Bayes Model Definition
nb = NaiveBayes(labelCol="label", featuresCol="features")
# Defining Hyperparameter Grid for Naive Bayes Model
paramGrid = ParamGridBuilder().addGrid(nb.smoothing, [0.5, 1.0, 2.0]).build()
# Setting Up Cross-Validation for Naive Bayes Model
nb_cv = CrossValidator(estimator=nb, estimatorParamMaps=paramGrid, evaluator=evaluator, numFolds=3)
# Training, Testing, and Evaluating the Naive Bayes Model
# Start time for training
start_train_time = time.time()
# Train model
nb_cv_model = nb_cv.fit(train_data)
# End time for training
end_train_time = time.time()
train_duration = end_train_time - start_train_time
print(f"Training Duration: {train_duration:.2f} seconds")
# Start time for testing
start_test_time = time.time()
# Test model
nb_predictions = nb_cv_model.bestModel.transform(test_data)
nb_accuracy = evaluator.evaluate(nb_predictions)
# End time for testing
end_test_time = time.time()
test_duration = end_test_time - start_test_time
print(f"Testing Duration: {test_duration:.2f} seconds")
# Output results
print(f"Naive Bayes Accuracy: {nb_accuracy:.4f}")
# Output the best hyperparameters for Naive Bayes
print(f"Best Naive Bayes Hyperparameters: ")
best_params = nb_cv_model.bestModel.extractParamMap()
for param, value in best_params.items():
print(f"{param.name}: {value}")
# Defining SVM Model
svm = LinearSVC(labelCol="label", featuresCol="features")
# Defining Hyperparameter Grid for SVM Model
paramGrid = ParamGridBuilder().addGrid(svm.maxIter, [50, 100]) \
.addGrid(svm.regParam, [0.01, 0.1]) \
.build()
# Setting Up Cross-Validation for SVM Model
svm_cv = CrossValidator(estimator=svm, estimatorParamMaps=paramGrid, evaluator=evaluator, numFolds=3)
# Training, Testing, and Evaluating the SVM Model
# Start time for training
start_train_time = time.time()
# Train model
svm_cv_model = svm_cv.fit(train_data)
# End time for training
end_train_time = time.time()
train_duration = end_train_time - start_train_time
print(f"Training Duration: {train_duration:.2f} seconds")
# Start time for testing
start_test_time = time.time()
# Test model
svm_predictions = svm_cv_model.bestModel.transform(test_data)
svm_accuracy = evaluator.evaluate(svm_predictions)
# End time for testing
end_test_time = time.time()
test_duration = end_test_time - start_test_time
print(f"Testing Duration: {test_duration:.2f} seconds")
# Output results
print(f"SVM Accuracy: {svm_accuracy:.4f}")
# Output the best hyperparameters for SVM
print(f"Best SVM Hyperparameters: ")
best_params = svm_cv_model.bestModel.extractParamMap()
for param, value in best_params.items():
print(f"{param.name}: {value}")
# Model Comparison and Evaluation: Random Forest, Decision Tree, Naive Bayes, and SVM
# Define a function to evaluate metrics for each model
def evaluate_model(model_name, model, test_data):
# Generate predictions
predictions = model.transform(test_data)
# Initialize evaluators for each metric
accuracy_evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
precision_evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="weightedPrecision")
recall_evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="weightedRecall")
f1_evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="f1")
# Calculate metrics
accuracy = accuracy_evaluator.evaluate(predictions)
precision = precision_evaluator.evaluate(predictions)
recall = recall_evaluator.evaluate(predictions)
f1 = f1_evaluator.evaluate(predictions)
# Return results as a dictionary
return {
"Model": model_name,
"Accuracy": accuracy,
"Precision": precision,
"Recall": recall,
"F1-Score": f1,
}
# Evaluate all models
results = []
# Random Forest
results.append(evaluate_model("Random Forest", rf_cv_model.bestModel, test_data))
# Decision Tree
results.append(evaluate_model("Decision Tree", dt_cv_model.bestModel, test_data))
# Naive Bayes
results.append(evaluate_model("Naive Bayes", nb_cv_model.bestModel, test_data))
# SVM
results.append(evaluate_model("SVM", svm_cv_model.bestModel, test_data))
# Display results in a sorted order based on F1-Score
results_sorted = sorted(results, key=lambda x: x["F1-Score"], reverse=True)
# Print results
print("\nComparison of Models:")
for result in results_sorted:
print(f"Model: {result['Model']}")
print(f" Accuracy: {result['Accuracy']:.4f}")
print(f" Precision: {result['Precision']:.4f}")
print(f" Recall: {result['Recall']:.4f}")
print(f" F1-Score: {result['F1-Score']:.4f}")
print()
# Identify the best model
best_model = results_sorted[0]
print(f"Best Model: {best_model['Model']}")
print(f" Accuracy: {best_model['Accuracy']:.4f}")
print(f" Precision: {best_model['Precision']:.4f}")
print(f" Recall: {best_model['Recall']:.4f}")
print(f" F1-Score: {best_model['F1-Score']:.4f}")
# Confusion Matrix Comparison for Phishing Detection Models
# Function to calculate confusion matrix
def get_confusion_matrix(model, test_data):
predictions = model.transform(test_data)
# Ensure the prediction column is in integer format
confusion_matrix = predictions.groupBy("label", "prediction").count().toPandas()
# Labels for confusion matrix (0 = Phishing, 1 = Legitimate)
labels = [0, 1]
# Initialize the matrix with zeros
matrix = np.zeros((len(labels), len(labels)), dtype=int)
# Populate the confusion matrix
for _, row in confusion_matrix.iterrows():
actual = int(row["label"]) # Convert label to integer
predicted = int(row["prediction"]) # Convert prediction to integer
matrix[actual, predicted] = row["count"]
return matrix, labels
# Function to plot a confusion matrix heatmap
def plot_confusion_matrix(matrix, labels, model_name, ax):
sns.heatmap(matrix, annot=True, fmt='d', cmap="Blues", xticklabels=labels, yticklabels=labels, ax=ax)
ax.set_title(f"Confusion Matrix for {model_name}")
ax.set_xlabel("Predicted")
ax.set_ylabel("Actual")
fig, axes = plt.subplots(2, 2, figsize=(10, 7))
# List to store confusion matrices for each model
confusion_matrices = []
# Generate confusion matrices for each model
models = [
("Random Forest", rf_cv_model.bestModel),
("Decision Tree", dt_cv_model.bestModel),
("Naive Bayes", nb_cv_model.bestModel),
("SVM", svm_cv_model.bestModel),
]
# Plot confusion matrices for each model
for idx, (model_name, model) in enumerate(models):
matrix, labels = get_confusion_matrix(model, test_data)
row = idx // 2 # Determine row (0 or 1)
col = idx % 2 # Determine column (0 or 1)
plot_confusion_matrix(matrix, labels, model_name, axes[row, col])
# Adjust layout for better spacing
plt.tight_layout()
plt.show()
# Displaying Feature Importances from Best Model (Random Forest Model)
# Access feature importances from the trained Random Forest model
importances = rf_cv_model.bestModel.featureImportances.toArray()
# Map features to their importance scores
feature_importances = [(feature, importance) for feature, importance in zip(feature_columns, importances)]
# Sort and display feature importances
sorted_features = sorted(feature_importances, key=lambda x: x[1], reverse=True)
for feature, importance in sorted_features:
print(f"{feature}: {importance:.4f}")
# Saving the Best Random Forest Model in PySpark
best_model = rf_cv_model.bestModel
spark.stop()