-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeystrokeML.py
More file actions
154 lines (119 loc) · 5.22 KB
/
Copy pathKeystrokeML.py
File metadata and controls
154 lines (119 loc) · 5.22 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
#!/usr/bin/python3
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.neural_network import MLPClassifier
import joblib
import time
class KeystrokeML:
def __init__(self):
self.data = ""
self.pressFlightCol = []
self.holdCol = []
self.releasePressFlightCol = []
self.trainData = []
self.testData = []
self.predictData = []
def __call__(self):
self.data = ""
self.pressFlightCol = []
self.holdCol = []
self.releasePressFlightCol = []
self.trainData = []
self.testData = []
self.predictData = []
#20210105 MH: Read CSV
def setDataset(self, path):
self.data = pd.read_csv(path, header=0)
#20210105 MH: Create line plot to compare average time keystroke
def plotKeystrokes(self):
for ks in self.data.columns:
if ks.startswith("DD"):
self.pressFlightCol.append(ks)
elif ks.startswith("UD"):
self.releasePressFlightCol.append(ks)
elif ks.startswith("H"):
self.holdCol.append(ks)
plot = self.data[self.holdCol]
plot['subject'] = self.data['subject'].values
plot = plot.groupby('subject').mean()
plot.iloc[:6].T.plot(figsize=(10,6), title= 'Hold Time: Tiempo Promedio')
plot1 = self.data[self.pressFlightCol]
plot1['subject'] = self.data['subject'].values
plot1 = plot1.groupby('subject').mean()
plot1.iloc[:6].T.plot(figsize=(10,6), title= 'Press Flight Time: Tiempo Promedio')
plot2 = self.data[self.releasePressFlightCol]
plot2['subject'] = self.data['subject'].values
plot2 = plot2.groupby('subject').mean()
plot2.iloc[:6].T.plot(figsize=(10,6), title= 'Release-Press Flight Time: Tiempo Promedio')
#20210105 MH: Show information about the dataset
def exploreData(self):
print("Dataset Information")
print(self.data.info())
print("Dataset Describe")
print(self.data.describe())
#20210105 MH: Evaluates the accuracy prediction level of 3 types of ML classifiers
def evaluateClassifier(self):
#20210105 MH: split dataset in train and test data
self.trainData, self.testData = train_test_split(self.data, test_size=0.2, random_state=0)
#20210105 MH: asigning train and test data axis
x_train = self.trainData[self.data.columns[2:]]
y_train = self.trainData['subject']
x_test = self.testData[self.data.columns[2:]]
y_test = self.testData['subject']
#20210105 MH: Evaluating KNN Classifier
knc = KNeighborsClassifier()
knc.fit(x_train, y_train)
y_pred = knc.predict(x_test)
knc_accuracy = metrics.accuracy_score(y_test, y_pred)
print("KNN Classifier Accuracy: ", round(knc_accuracy*100,2), "%")
#20210105 MH: Evaluating Support Vector Linear Classifier
svc = svm.SVC(kernel='linear')
svc.fit(x_train, y_train)
y_pred = svc.predict(x_test)
svc_accuracy = metrics.accuracy_score(y_test, y_pred)
print("Support Vector Linear Classifier Accuracy: " , round(svc_accuracy*100,2), "%")
#20210105 MH: Evaluating Multilayer Perceptron Classifier
mlp = MLPClassifier()
mlp.fit(x_train, y_train)
y_pred = mlp.predict(x_test)
mlp_accuracy = metrics.accuracy_score(y_test, y_pred)
print("Multilayer Perceptron Classifier Accuracy: " , round(mlp_accuracy*100,2), "%")
def trainModel(self):
tCls = input("Enter 0=KNN, 1=Sample Vector Linear, 2=Multilayer Perceptron: ")
if tCls == "0":
#KNN
print("0")
elif tCls == "1":
#SVL
print("1")
elif tCls == "2":
#MLP
#20210105 MH: split dataset in train and tes data
self.trainData, self.testData = train_test_split(self.data, test_size=0.2, random_state=0)
#20210105 MH: asigning train and test data axis
x_train = self.trainData[self.data.columns[2:]]
y_train = self.trainData['subject']
#x_test = self.testData[self.data.columns[2:]]
#y_test = self.testData['subject']
#20210105 MH: Training Multilayer Perceptron Classifier
mlp = MLPClassifier()
mlp.fit(x_train, y_train)
joblib.dump(mlp, './TrainedModels/MLPClassifierTrained.pkl')
else:
print("Error: Selected value is not valid!")
def predictFromFile(self, path):
self.predictData = pd.read_csv(path, header=0)
x_pred = self.predictData[self.predictData.columns[2:]]
mlp = joblib.load('./01_KeystrokeDynamics/TrainedModels/MLPClassifierTrained.pkl')
y_pred = mlp.predict(x_pred)
print("Input keystrokes dynamics: ")
print(x_pred)
time.sleep(2.0)
print("Keystrokes dynamics have been evaluated!")
time.sleep(2.0)
print("Keystrokes dynamics owner: ", y_pred)