-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_single.py
51 lines (46 loc) · 1.68 KB
/
svm_single.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
import pandas as pd
from sklearn.cluster import KMeans
import math
from sklearn.svm import OneClassSVM
import numpy as np
from sklearn.metrics import roc_curve
msr = []
far = []
def getEER(user_scores, imposter_scores):
#print user_scores
labels = [0]*len(user_scores) + [1]*len(imposter_scores)
fpr, tpr, thresholds = roc_curve(labels, user_scores + imposter_scores)
missrates = 1 - tpr
farates = fpr
msr.append(np.mean(missrates))
far.append(np.mean(farates))
dists = missrates - farates
idx1 = np.argmin(dists[dists >= 0])
idx2 = np.argmax(dists[dists < 0])
x = [missrates[idx1], farates[idx1]]
y = [missrates[idx2], farates[idx2]]
a = ( x[0] - x[1] ) / ( y[1] - x[1] - y[0] + x[0] )
eer = x[0] + a * ( y[0] - x[0] )
return eer
data = pd.read_csv("/home/ravi/Documents/DWDM/Project/data/DSL-StrongPasswordData.csv")
subjects = data["subject"].unique()
EERS = []
for subject in subjects:
genuine = data.loc[data.subject == subject, "H.period":"H.Return"]
genuine_train = genuine.head(300)
genuine_test = genuine.tail(100)
imposter = data.loc[data.subject != subject, "H.period":"H.Return"]
test = genuine_test.append(imposter)
clf = OneClassSVM(kernel='rbf', gamma=26)
clf.fit(genuine_train)
user_scores = -clf.decision_function(genuine_test)
imposter_scores = -clf.decision_function(imposter)
#print user_scores
#print imposter_scores
curr_eer = getEER(list(user_scores), list(imposter_scores))
EERS.append(curr_eer)
print "Subject:", subject, "EER: ", curr_eer
#exit(0)
print "\n\nOverall Equal Error Rate:",np.mean(EERS)
print "Miss Rate:", np.mean(msr)
print "False Rate: ",np.mean(far)