-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
413 lines (374 loc) · 18.8 KB
/
run.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
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report, precision_score
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn import preprocessing, svm
from sklearn.neural_network import MLPClassifier
from numpy import reshape, mean, std, var, cov, abs, power, sqrt
from timeit import default_timer as timer
from scipy.stats import iqr, skew, kurtosis
from scipy.fftpack import fft
NO_OF_FILES = 65
NO_OF_FEATURES = 9
GENUINE_LABEL = 1
FRAUDULENT_LABEL = 0
NO_ROWS_TRAINING_100_HZ = 162000
NO_ROWS_TESTING_100_HZ = 54000
ROWS_PER_WINDOW_100_HZ_5_SECONDS = 500
ROWS_PER_WINDOW_100_HZ_1_SECOND = 100
NO_ROWS_TRAINING_25_HZ = 40500
NO_ROWS_TESTING_25_HZ = 13500
ROWS_PER_WINDOW_25_HZ_5_SECONDS = 125
ROWS_PER_WINDOW_25_HZ_1_SECOND = 25
HZ = 100
WINDOW = 5
CLASSIFIER = 'Random Forest'
SCALING = 'Standardization'
#Normalization
FEATURE_ENGINEERING = True
CORRELATIONS = True
LENGTHS_ANGLES = True
#For 5 second windows -> 162000 rows total, 500 rows per window -> 1st half genuine & second half fraudulent
def create_window(data, no_rows_per_file, no_rows_per_window):
windows = []
for i in range(0,int(no_rows_per_file/2),no_rows_per_window):
window = []
for j in range(NO_OF_FEATURES):
window.append(data.iloc[i:i + no_rows_per_window, j].values)
window.append(GENUINE_LABEL)
windows.append(window)
for i in range(int(no_rows_per_file/2), no_rows_per_file,no_rows_per_window):
window = []
for j in range(NO_OF_FEATURES):
window.append(data.iloc[i:i + no_rows_per_window, j].values)
window.append(FRAUDULENT_LABEL)
windows.append(window)
return windows
def create_window_FE(data, no_rows_per_file, no_rows_per_window):
windows = []
for i in range(0,int(no_rows_per_file/2),no_rows_per_window):
window, means, SDs, accelerometer, gyroscope, magnetometer = [], [], [], [], [], []
for j in range(NO_OF_FEATURES):
if LENGTHS_ANGLES:
if j < 3:
accelerometer.extend(data.iloc[i:i + no_rows_per_window, j].values)
elif 3 <= j < 6:
gyroscope.extend(data.iloc[i:i + no_rows_per_window, j].values)
else:
magnetometer.extend(data.iloc[i:i + no_rows_per_window, j].values)
feature_raw = data.iloc[i:i + no_rows_per_window, j].values
window.extend(fe_stats(feature_raw))
means.append(mean(feature_raw))
SDs.append(std(feature_raw))
if j != 0 and j % 3 == 0 and CORRELATIONS or j == 8 and CORRELATIONS:
window.append(correlation(means[0], means[1], SDs[0], SDs[1]))
window.append(correlation(means[0], means[2], SDs[0], SDs[2]))
window.append(correlation(means[1], means[2], SDs[1], SDs[2]))
means, SDs = [], []
means.append(mean(feature_raw))
SDs.append(std(feature_raw))
if LENGTHS_ANGLES:
accelerometer_lengths = calculate_lengths_of_vectors(accelerometer, int(no_rows_per_window))
gyroscope_lengths = calculate_lengths_of_vectors(gyroscope, int(no_rows_per_window))
magnetometer_lengths = calculate_lengths_of_vectors(magnetometer, int(no_rows_per_window))
accelerometer_angles = calculate_angles(accelerometer, accelerometer_lengths, int(no_rows_per_window))
gyroscope_angles = calculate_angles(gyroscope, gyroscope_lengths, int(no_rows_per_window))
magnetometer_angles = calculate_angles(magnetometer, magnetometer_lengths, int(no_rows_per_window))
window.append(mean(accelerometer_lengths))
window.append(mean(gyroscope_lengths))
window.append(mean(magnetometer_lengths))
for j in range(3):
window.append(mean(accelerometer_angles[j]))
window.append(mean(gyroscope_angles[j]))
window.append(mean(magnetometer_angles[j]))
window.append(GENUINE_LABEL)
windows.append(window)
for i in range(int(no_rows_per_file/2), no_rows_per_file,no_rows_per_window):
window, means, SDs, accelerometer, gyroscope, magnetometer = [], [], [], [], [], []
for j in range(NO_OF_FEATURES):
if LENGTHS_ANGLES:
if j < 3:
accelerometer.extend(data.iloc[i:i + no_rows_per_window, j].values)
elif 3 <= j < 6:
gyroscope.extend(data.iloc[i:i + no_rows_per_window, j].values)
else:
magnetometer.extend(data.iloc[i:i + no_rows_per_window, j].values)
feature_raw = data.iloc[i:i + no_rows_per_window, j].values
window.extend(fe_stats(feature_raw))
means.append(mean(feature_raw))
SDs.append(std(feature_raw))
if j != 0 and j % 3 == 0 and CORRELATIONS or j == 8 and CORRELATIONS:
window.append(correlation(means[0], means[1], SDs[0], SDs[1]))
window.append(correlation(means[0], means[2], SDs[0], SDs[2]))
window.append(correlation(means[1], means[2], SDs[1], SDs[2]))
means, SDs = [], []
means.append(mean(feature_raw))
SDs.append(std(feature_raw))
if LENGTHS_ANGLES:
accelerometer_lengths = calculate_lengths_of_vectors(accelerometer, int(no_rows_per_window))
gyroscope_lengths = calculate_lengths_of_vectors(gyroscope, int(no_rows_per_window))
magnetometer_lengths = calculate_lengths_of_vectors(magnetometer, int(no_rows_per_window))
accelerometer_angles = calculate_angles(accelerometer, accelerometer_lengths, int(no_rows_per_window))
gyroscope_angles = calculate_angles(gyroscope, gyroscope_lengths, int(no_rows_per_window))
magnetometer_angles = calculate_angles(magnetometer, magnetometer_lengths, int(no_rows_per_window))
window.append(mean(accelerometer_lengths))
window.append(mean(gyroscope_lengths))
window.append(mean(magnetometer_lengths))
for j in range(3):
window.append(mean(accelerometer_angles[j]))
window.append(mean(gyroscope_angles[j]))
window.append(mean(magnetometer_angles[j]))
window.append(FRAUDULENT_LABEL)
windows.append(window)
return windows
def fe_stats(feature):
feature_stats = []
feature_stats.append(max(feature))
feature_stats.append(min(feature))
feature_stats.append(mean(feature))
feature_stats.append(std(feature))
feature_stats.append(iqr(feature))
feature_stats.append(var(feature))
feature_stats.append(skew(feature))
feature_stats.append(kurtosis(feature))
feature_stats.append(calculate_energy(feature))
return feature_stats
def correlation(X, Y, sd_x, sd_y):
input_cov = [X, Y]
if (sd_x * sd_y) != 0:
return cov(input_cov)/(sd_x * sd_y)
else:
print('Can not calculate correlation')
return 0
def calculate_energy(feature):
energy = 0
dft_values = fft(feature)
for i in range(0,len(feature)):
energy = energy + power(abs(dft_values[i]), 2)
energy = energy/len(feature)
return energy
def calculate_lengths_of_vectors(instrument_measures, no_of_rows):
lengths = []
for i in range(0, no_of_rows):
x = instrument_measures[i]
y = instrument_measures[i + no_of_rows]
z = instrument_measures[i + (no_of_rows * 2)]
lengths.append(calculate_vector_length(x, y, z))
return lengths
def calculate_vector_length(x, y, z):
return sqrt(power(x, 2) + power(y, 2) + power(z, 2))
def calculate_angles(instrument_measures, lengths, no_of_rows):
x_angles = []
y_angles = []
z_angles = []
for i in range(no_of_rows):
x_angles.append(instrument_measures[i]/lengths[i])
y_angles.append(instrument_measures[i + no_of_rows]/lengths[i])
z_angles.append(instrument_measures[i + no_of_rows*2]/lengths[i])
return [x_angles, y_angles, z_angles]
def load_experiment(start, end, experiment, frequency):
for i in range(start, end):
training_files.append('Supervised_'+ str(frequency) +'_Hz/train_activity_experiment_f'+ str(frequency) +'_acc_exp' + str(experiment) + '_' + str(i) + '.csv')
testing_files.append('Supervised_'+ str(frequency) +'_Hz/test_activity_experiment_f'+ str(frequency) +'_acc_exp' + str(experiment) + '_' + str(i) + '.csv')
def load_experiment_25_Hz(start, end, experiment):
for i in range(start, end):
training_files.append('Supervised_25_Hz/train_activity_experiment_f25_acc_exp' + str(experiment) + '_' + str(i) + '.csv')
testing_files.append('Supervised_25_Hz/test_activity_experiment_f25_acc_exp' + str(experiment) + '_' + str(i) + '.csv')
def load_data(frequency):
print('Loading data')
load_experiment(10, 21, 1, frequency)
load_experiment(20, 31, 2, frequency)
load_experiment(30, 40, 3, frequency)
# Fails on experiment 3 34 both frequencies
load_experiment(40, 51, 4, frequency)
load_experiment(50, 61, 5, frequency)
load_experiment(60, 71, 6, frequency)
def seperate_data_from_labels(window, x, y):
x_internal = []
y_internal = []
for i in range(len(window)):
x_internal.append(window[i][:-1])
y_internal.append(window[i][-1])
x.append(x_internal)
y.append(y_internal)
def scaler_training(scaler, train_file):
train_file_scaled = scaler.fit_transform(train_file)
train_file = pd.DataFrame(train_file_scaled)
return train_file
def scaler_testing(scaler, test_file):
test_file_scaled = scaler.transform(test_file)
test_file = pd.DataFrame(test_file_scaled)
return test_file
def train_user(classifier, x, y, no_of_windows, rows_per_window):
x_input = reshape(x, [no_of_windows, rows_per_window])
classifier.fit(x_input, y)
def test_user(classifier, x, no_of_windows, rows_per_window):
x_input = reshape(x, [no_of_windows, rows_per_window])
predictions = classifier.predict(x_input)
return predictions
def normalization_linear_scaler(current_train_file, current_test_file):
if SCALING == 'Normalization':
training_data.append(scaler_training(min_max_scaler, current_train_file))
testing_data.append(scaler_testing(min_max_scaler, current_test_file))
else:
training_data.append(scaler_training(linear_scaler_to_unit_variance, current_train_file))
testing_data.append(scaler_testing(linear_scaler_to_unit_variance, current_test_file))
if __name__== '__main__':
training_files, testing_files, training_data, testing_data, x, y, test_x, test_y = [], [], [], [], [], [], [], []
min_max_scaler = preprocessing.MinMaxScaler()
linear_scaler_to_unit_variance = preprocessing.StandardScaler()
load_data(HZ)
print('Second Loop')
for i in range(NO_OF_FILES):
current_train_file = pd.read_csv(training_files[i])
current_test_file = pd.read_csv(testing_files[i])
print('Scaling and windowing user:', i)
if FEATURE_ENGINEERING:
if HZ == 100:
if WINDOW == 5:
# 5 second window 100 Hz
training_window = create_window_FE(current_train_file, NO_ROWS_TRAINING_100_HZ,
ROWS_PER_WINDOW_100_HZ_5_SECONDS)
testing_window = create_window_FE(current_test_file, NO_ROWS_TESTING_100_HZ,
ROWS_PER_WINDOW_100_HZ_5_SECONDS)
else:
# 1 second window 100 Hz
training_window = create_window_FE(current_train_file, NO_ROWS_TRAINING_100_HZ,
ROWS_PER_WINDOW_100_HZ_1_SECOND)
testing_window = create_window_FE(current_test_file, NO_ROWS_TESTING_100_HZ,
ROWS_PER_WINDOW_100_HZ_1_SECOND)
else:
if WINDOW == 5:
# 5 second window 25 Hz
training_window = create_window_FE(current_train_file, NO_ROWS_TRAINING_25_HZ,
ROWS_PER_WINDOW_25_HZ_5_SECONDS)
testing_window = create_window_FE(current_test_file, NO_ROWS_TESTING_25_HZ,
ROWS_PER_WINDOW_25_HZ_5_SECONDS)
else:
# 1 Second Window 25 Hz
training_window = create_window_FE(current_train_file, NO_ROWS_TRAINING_25_HZ,
ROWS_PER_WINDOW_25_HZ_1_SECOND)
testing_window = create_window_FE(current_test_file, NO_ROWS_TESTING_25_HZ,
ROWS_PER_WINDOW_25_HZ_1_SECOND)
normalization_linear_scaler(training_window, testing_window)
else:
normalization_linear_scaler(current_train_file, current_test_file)
if HZ == 100:
if WINDOW == 5:
# 5 second window 100 Hz
training_window = create_window(training_data[i], NO_ROWS_TRAINING_100_HZ,
ROWS_PER_WINDOW_100_HZ_5_SECONDS)
testing_window = create_window(testing_data[i], NO_ROWS_TESTING_100_HZ,
ROWS_PER_WINDOW_100_HZ_5_SECONDS)
else:
# 1 second window 100 Hz
training_window = create_window(training_data[i], NO_ROWS_TRAINING_100_HZ,
ROWS_PER_WINDOW_100_HZ_1_SECOND)
testing_window = create_window(testing_data[i], NO_ROWS_TESTING_100_HZ, ROWS_PER_WINDOW_100_HZ_1_SECOND)
else:
if WINDOW == 5:
# 5 second window 25 Hz
training_window = create_window(training_data[i], NO_ROWS_TRAINING_25_HZ,
ROWS_PER_WINDOW_25_HZ_5_SECONDS)
testing_window = create_window(testing_data[i], NO_ROWS_TESTING_25_HZ, ROWS_PER_WINDOW_25_HZ_5_SECONDS)
else:
# 1 Second Window 25 Hz
training_window = create_window(training_data[i], NO_ROWS_TRAINING_25_HZ,
ROWS_PER_WINDOW_25_HZ_1_SECOND)
testing_window = create_window(testing_data[i], NO_ROWS_TESTING_25_HZ, ROWS_PER_WINDOW_25_HZ_1_SECOND)
seperate_data_from_labels(training_window, x, y)
seperate_data_from_labels(testing_window, test_x, test_y)
if CLASSIFIER == 'Random Forest':
classifier = RandomForestClassifier(n_estimators=250, random_state=101)
elif CLASSIFIER == 'SVM':
classifier = svm.SVC(kernel='rbf', C=100, gamma=100, random_state=101)
elif CLASSIFIER == 'Logistic Regression':
classifier = LogisticRegression(C=100, random_state=101)
else:
classifier = MLPClassifier(hidden_layer_sizes=(72,36), activation='logistic', random_state=101, alpha=0.01, max_iter=400)
precision, avg_train_time, avg_test_time = 0, 0, 0
for i in range(NO_OF_FILES):
print('Training User',i+1)
start_time = timer()
if FEATURE_ENGINEERING:
if CORRELATIONS:
if LENGTHS_ANGLES:
if WINDOW == 5:
train_user(classifier, x[i], y[i], 324, 102)
else:
test_user(classifier, x[i], y[i], 1620, 102)
else:
if WINDOW == 5:
train_user(classifier, x[i], y[i], 324, 90)
else:
train_user(classifier, x[i], y[i], 1620, 90)
else:
if WINDOW == 5:
train_user(classifier, x[i], y[i], 324, 81)
else:
train_user(classifier, x[i], y[i], 1620, 81)
elif HZ == 100:
if WINDOW == 5:
#5 Seconds Training 100 Hz
train_user(classifier, x[i], y[i], 324, 4500)
else:
# 1 Second Training 100 Hz
train_user(classifier, x[i], y[i], 1620, 900)
else:
if WINDOW == 5:
# 5 Seconds Training 25 Hz
train_user(classifier, x[i], y[i], 324, 1125)
else:
#1 Second Training 25Hz
train_user(classifier, x[i], y[i], 1620, 225)
end_time = timer()
training_time = end_time - start_time
print('Time to train user',i+1, ':', training_time)
print('Testing User ',i+1)
start_time = timer()
if FEATURE_ENGINEERING:
if CORRELATIONS:
if LENGTHS_ANGLES:
if WINDOW == 5:
predictions = test_user(classifier, test_x[i], 108, 102)
else:
predictions = test_user(classifier, test_x[i], 540, 102)
else:
if WINDOW == 5:
predictions = test_user(classifier, test_x[i], 108, 90)
else:
predictions = test_user(classifier, test_x[i], 540, 90)
else:
if WINDOW == 5:
predictions = test_user(classifier, test_x[i], 108, 81)
else:
predictions = test_user(classifier, test_x[i], 540, 81)
elif HZ == 100:
if WINDOW == 5:
# 5 Seconds Testing 100 Hz
predictions = test_user(classifier, test_x[i], 108, 4500)
else:
# 1 Second Testing 100 Hz
predictions = test_user(classifier, test_x[i], 540, 900)
else:
if WINDOW == 5:
# 5 Seconds Testing 25
predictions = test_user(classifier, test_x[i], 108, 1125)
else:
# 1 Second Testing 25 Hz
predictions = test_user(classifier, test_x[i], 540, 225)
end_time = timer()
testing_time = end_time - start_time
print('Time to test user', i + 1, ':', testing_time)
precision = precision + precision_score(test_y[i], predictions)
avg_train_time = avg_train_time + training_time
avg_test_time = avg_test_time + testing_time
#confusion = confusion_matrix(actual, predictions)
#print(confusion)
#print(classification_report(actual, predictions))
#print(precision_score(actual, predictions))
precision = precision / NO_OF_FILES
avg_train_time = avg_train_time / NO_OF_FILES
avg_test_time = avg_test_time / NO_OF_FILES
print('Accuracy:', precision, "\nAverage Training Time Per User:", avg_train_time, "\nAverage Testing Time Per User:", avg_test_time)