-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keystroke_GMM.py
59 lines (48 loc) · 1.99 KB
/
Keystroke_GMM.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
#keystroke_GMM.py
from sklearn.mixture import GMM
import pandas
from EER_GMM import evaluateEER
import numpy as np
import warnings
warnings.filterwarnings("ignore")
class GMMDetector:
#the training(), testing() and evaluateEER() function change, rest all is same.
def __init__(self, subjects):
self.user_scores = []
self.imposter_scores = []
self.mean_vector = []
self.subjects = subjects
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)
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()
eers.append(evaluateEER(self.user_scores, \
self.imposter_scores))
return np.mean(eers)
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 GMM detector:"
print(GMMDetector(subjects).evaluate())