-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllDetectors.py
More file actions
186 lines (160 loc) · 6.63 KB
/
AllDetectors.py
File metadata and controls
186 lines (160 loc) · 6.63 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from scipy.spatial.distance import cityblock, euclidean
import numpy as np
np.set_printoptions(suppress = True)
import pandas
from EER import evaluateEER
from EER_GMM import evaluateEERGMM
from sklearn.svm import OneClassSVM
from sklearn.mixture import GMM
import warnings
warnings.filterwarnings("ignore")
from abc import ABCMeta, abstractmethod
import os
class Detector:
"""
A generic class with all the common functionalities.
"""
__metaclass__ = ABCMeta
def __init__(self, subjects):
self.user_scores = []
self.imposter_scores = []
self.mean_vector = []
self.subjects = subjects
@abstractmethod
def training(self):
pass
@abstractmethod
def testing(self):
pass
def evaluate(self):
eers = []
for subject in subjects:
genuine_user_data = data.loc[data.subject == subject, \
"H.period":"H.Return"]
imposter_data = data.loc[data.subject != subject, :]
self.train = genuine_user_data[:200]
self.test_genuine = genuine_user_data[200:]
self.test_imposter = imposter_data.groupby("subject"). \
head(5).loc[:, "H.period":"H.Return"]
self.training()
self.testing()
if isinstance(self, GMMDetector):
eers.append(evaluateEERGMM(self.user_scores, \
self.imposter_scores))
else:
eers.append(evaluateEER(self.user_scores, \
self.imposter_scores))
return np.mean(eers), np.std(eers)
class ManhattanDetector(Detector):
"""
A class for Manhattan detector model.
"""
def training(self):
self.mean_vector = self.train.mean().values
def testing(self):
for i in range(self.test_genuine.shape[0]):
cur_score = cityblock(self.test_genuine.iloc[i].values, \
self.mean_vector)
self.user_scores.append(cur_score)
for i in range(self.test_imposter.shape[0]):
cur_score = cityblock(self.test_imposter.iloc[i].values, \
self.mean_vector)
self.imposter_scores.append(cur_score)
class ManhattanFilteredDetector(Detector):
"""
A class for Manhatten Filtered detector model.
"""
def training(self):
self.mean_vector = self.train.mean().values
self.std_vector = self.train.std().values
dropping_indices = []
for i in range(self.train.shape[0]):
cur_score = euclidean(self.train.iloc[i].values,
self.mean_vector)
if (cur_score > 3*self.std_vector).all() == True:
dropping_indices.append(i)
self.train = self.train.drop(self.train.index[dropping_indices])
self.mean_vector = self.train.mean().values
def testing(self):
for i in range(self.test_genuine.shape[0]):
cur_score = cityblock(self.test_genuine.iloc[i].values, \
self.mean_vector)
self.user_scores.append(cur_score)
for i in range(self.test_imposter.shape[0]):
cur_score = cityblock(self.test_imposter.iloc[i].values, \
self.mean_vector)
self.imposter_scores.append(cur_score)
class ManhattanScaledDetector(Detector):
"""
A class for Manhattan Scaled detector model.
"""
def training(self):
self.mean_vector = self.train.mean().values
self.mad_vector = self.train.mad().values
def testing(self):
for i in range(self.test_genuine.shape[0]):
cur_score = 0
for j in range(len(self.mean_vector)):
cur_score = cur_score + \
abs(self.test_genuine.iloc[i].values[j] - \
self.mean_vector[j]) / self.mad_vector[j]
self.user_scores.append(cur_score)
for i in range(self.test_imposter.shape[0]):
cur_score = 0
for j in range(len(self.mean_vector)):
cur_score = cur_score + \
abs(self.test_imposter.iloc[i].values[j] - \
self.mean_vector[j]) / self.mad_vector[j]
self.imposter_scores.append(cur_score)
class SVMDetector(Detector):
"""
A class for SVM detector model.
"""
def training(self):
self.clf = OneClassSVM(kernel='rbf',gamma=26)
self.clf.fit(self.train)
def testing(self):
self.user_scores = -self.clf.decision_function(self.test_genuine)
self.imposter_scores = -self.clf.decision_function(self.test_imposter)
self.user_scores = list(self.user_scores)
self.imposter_scores = list(self.imposter_scores)
class GMMDetector(Detector):
"""
A class for GMM detector model.
"""
def training(self):
self.gmm = GMM(n_components = 2, covariance_type = 'diag',
verbose = False )
self.gmm.fit(self.train)
def testing(self):
for i in range(self.test_genuine.shape[0]):
j = self.test_genuine.iloc[i].values
cur_score = self.gmm.score([j])
self.user_scores.append(cur_score)
for i in range(self.test_imposter.shape[0]):
j = self.test_imposter.iloc[i].values
cur_score = self.gmm.score([j])
self.imposter_scores.append(cur_score)
path = os.path.dirname( os.path.realpath(__file__) )
data_path = os.path.join(path, 'keystroke.csv')
data = pandas.read_csv(data_path)
subjects = data["subject"].unique()
print("average EER for Manhattan detector:")
obj = ManhattanDetector(subjects)
print(obj.evaluate())
print("=====================================================================")
print("average EER for Manhattan filtered detector:")
obj = ManhattanFilteredDetector(subjects)
print(obj.evaluate())
print("=====================================================================")
print("average EER for Manhattan scaled detector:")
obj = ManhattanScaledDetector(subjects)
print(obj.evaluate())
print("=====================================================================")
print("average EER for One class SVM detector:")
obj = SVMDetector(subjects)
print(obj.evaluate())
print("=====================================================================")
print("average EER for GMM detector:")
obj = GMMDetector(subjects)
print(obj.evaluate())