-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathANN.py
More file actions
172 lines (114 loc) · 4.51 KB
/
ANN.py
File metadata and controls
172 lines (114 loc) · 4.51 KB
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
## James Quintero
## https://github.com/JamesQuintero
## Created: 5/2019
## Modified: 4/2021
##
## Handles all the neural network modeling
import sys
import os
import time
import numpy as np
# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import LeakyReLU
from keras.layers import PReLU
from keras.layers import Bidirectional
from data_handler import DataHandler
class ANN:
data_handler = None
def __init__(self):
self.data_handler = DataHandler()
#input is a 2D list of unnormalized data
#output is binary/categorical list denoting DDoS type
def train_model(self, input_data, output_data, dataset_index):
train_size = 0.7 #percentage of dataset to use for training
#splits data into train and test datasets for cross validation
X_train = np.array(input_data[ : int(len(input_data)*train_size)])
y_train = np.array(output_data[ : int(len(output_data)*train_size)])
X_test = np.array(input_data[int(len(input_data)*train_size) : ])
y_test = np.array(output_data[int(len(output_data)*train_size) : ])
dataset_name = self.data_handler.get_dataset_filename(dataset_index)
model_path = "./Models/{}.h5".format(dataset_name)
#if model has never been trained, train it
if os.path.exists(model_path)==False:
print("Creating neural network")
# Initialising the ANN
model = Sequential()
# Adding the input layer and the first hidden layer
#the number of nodes in the input layer is the number of countries
#hidden layer has num_countries/2 nodes
model.add(Dense(input_dim = len(X_train[0]), units = int(len(X_train[0])/1), kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dropout(rate = 0.2))
model.add(Dense(units = int(len(X_train[0])/1), kernel_initializer = 'uniform', activation = 'relu'))
model.add(Dropout(rate = 0.2))
# Adding the output layer
#1 output layer node, since that'll be a percentage
model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
# Compiling the ANN
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
print("Training neural network")
model.fit(X_train, y_train, batch_size = 20, epochs = 20)
#saves the model for future use
model.save(model_path)
#if model has already been trained, load it
else:
print("Model already exists, so load it\n")
model = load_model(model_path)
start_time = time.time()
print("Testing neural network on hold-out portion of the dataset.")
# Predicting the Test set results
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5)
print("--- %s seconds to predict ---" % (time.time() - start_time))
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print("Confusion matrix: ")
print(str(cm))
if len(cm) == 1:
print("Undesirable confusion matrix, the neural network predicted on a single class for all data points. Try training with a better configuration, or testing with more occurances of both target classes.")
return
#True Negative
TN = cm[0][0]
#False Positive
FP = cm[0][1]
#False Negative
FN = cm[1][0]
#True Positive
TP = cm[1][1]
accuracy = (TN+TP)/(TN+FP+FN+TP)
precision = TP/(FP+TP)
sensitivity = TP/(TP+FN)
#when it's a downmove, how often does model predict a downmove?
specificity = TN/(TN+FP)
#want to get 200%, 100% for sensitivity and 100% for specificity
total = sensitivity + specificity
print("Accuracy: "+str(accuracy))
print("Precision: "+str(precision))
print("Sensitivity: "+str(sensitivity))
print("Specificity: "+str(specificity))
print("Total: "+str(total))
#model predicts labels, and results are saved to a csv
def predict(self, dataset_index, input_data):
#splits data into train and test datasets for cross validation
input_data = np.array(input_data)
dataset_name = self.data_handler.get_dataset_filename(dataset_index)
model_path = "./Models/{}.h5".format(dataset_name)
#if model has never been trained, train it
if os.path.exists(model_path):
model = load_model(model_path)
else:
print("Model {} doesn't exist".format(model_path))
return []
# Predicting the Test set results
y_pred = model.predict(input_data)
return y_pred
if __name__=="__main__":
neural_network = ANN()
neural_network.train_model([], [])