-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSVM.py
71 lines (57 loc) · 1.84 KB
/
SVM.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
import numpy as np
from numpy import array
from sklearn.svm import SVC
from sklearn.preprocessing import normalize
num_votes = 12
results = []
for vote_id in range(num_votes):
print("VOTE = ", vote_id)
# train label
train_y = []
read_label = open("features/train_label.txt", "r")
train_y = read_label.readlines()
train_y = [int(i) for i in train_y]
train_y = array(train_y)
# test label
y = []
read_label = open("features/label.txt", "r")
y = read_label.readlines()
y = [int(i) for i in y]
y = array(y)
# train featrue
train_X = []
read_feature = open("features/train_feature_"+str(vote_id)+".txt", "r")
lines = read_feature.readlines()
for line in lines:
line = line.split('\n')[0]
line = line.split(' ')
# print(line)
line = [float(i) for i in line]
train_X.append(line)
train_X = array(train_X)
# test featrue
X = []
read_feature = open("features/feature_"+str(vote_id)+".txt", "r")
lines = read_feature.readlines()
for line in lines:
line = line.split('\n')[0]
line = line.split(' ')
line = [float(i) for i in line]
X.append(line)
X = array(X)
# pick percentage of random points
# percentage = 100
# count = len(train_X) // percentage
# choice = np.random.randint(len(train_X), size=count)
# train_X = train_X[choice, :]
# train_y = train_y[choice]
clf = SVC(gamma='auto')
clf.fit(train_X, train_y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
results.append(clf.score(X, y))
print(clf.score(X, y))
results = array(results)
print('mean', np.mean(results))