-
Notifications
You must be signed in to change notification settings - Fork 21
/
svm.py
140 lines (109 loc) · 4.73 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
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
import numpy as np
import cvxopt
class SVM:
def __qp(self, X, y, kernel, C):
n_samples = X.shape[0]
P = np.outer(y, y) * kernel
q = np.full((n_samples, 1), -1.0)
G = np.vstack((-np.eye(n_samples), np.eye(n_samples)))
h = np.hstack((np.zeros(n_samples), np.full(n_samples, C)))
A = y.reshape((1, -1))
b = np.zeros(1)
res = cvxopt.solvers.qp(cvxopt.matrix(P), cvxopt.matrix(q), cvxopt.matrix(G), cvxopt.matrix(h), cvxopt.matrix(A), cvxopt.matrix(b))
alpha = np.array(res['x']).ravel()
support_items = np.flatnonzero(np.isclose(alpha, 0) == False)
self.__X_support = X[support_items]
self.__y_support = y[support_items]
self.__a_support = alpha[support_items]
free_items = np.flatnonzero(self.__a_support < C)
y_free = y[free_items]
self.__bias = y_free[0] - (self.__a_support * self.__y_support).T.dot(kernel[support_items, free_items[0]])
def __smo(self, X, y, kernel, C, epochs):
n_samples = X.shape[0]
alpha = np.zeros(n_samples)
self.__bias = 0
e = -y
for _ in range(epochs):
for i in range(n_samples):
hi = kernel[i].dot(alpha * y) + self.__bias
if (y[i] * hi < 1 and alpha[i] < C) or (y[i] * hi > 1 and alpha[i] > 0):
j = np.argmax(np.abs(e - e[i]))
if y[i] == y[j]:
L = max(0, alpha[i] + alpha[j] - C)
H = min(C, alpha[i] + alpha[j])
else:
L = max(0, alpha[j] - alpha[i])
H = min(C, C + alpha[j] - alpha[i])
if L == H:
continue
eta = kernel[i, i] + kernel[j, j] - 2 * kernel[i, j]
if eta <= 0:
continue
alpha_j = alpha[j] + y[j] * (e[i] - e[j]) / eta
if alpha_j > H:
alpha_j = H
elif alpha_j < L:
alpha_j = L
alpha_i = alpha[i] + y[i] * y[j] * (alpha[j] - alpha_j)
bi = self.__bias - e[i] - y[i] * kernel[i, i] * (alpha_i - alpha[i]) - y[j] * kernel[i, j] * (alpha_j - alpha[j])
bj = self.__bias - e[j] - y[i] * kernel[i, j] * (alpha_i - alpha[i]) - y[j] * kernel[j, j] * (alpha_j - alpha[j])
if 0 < alpha_i and alpha_i < C:
self.__bias = bi
elif 0 < alpha_j and alpha_j < C:
self.__bias = bj
else:
self.__bias = (bi + bj) / 2
alpha[i] = alpha_i
alpha[j] = alpha_j
e[i] = kernel[i].dot(alpha * y) + self.__bias - y[i]
e[j] = kernel[j].dot(alpha * y) + self.__bias - y[j]
support_items = np.flatnonzero(alpha > 1e-6)
self.__X_support = X[support_items]
self.__y_support = y[support_items]
self.__a_support = alpha[support_items]
def fit(self, X, y, kernel_func, C, solver, sigma=1, epochs=0):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
y : One-hot encoder, shape (n_samples,)
Target values, 1 or -1
kernel_func : kernel algorithm see also kernel.py
C : Penalty parameter C of the error term
solver : Solve algorithm
sigma : Parameter for rbf kernel
epochs : The number of epochs
'''
self.__sigma = sigma
self.__kernel_func = kernel_func
kernel = self.__kernel_func(X, X, self.__sigma)
if solver == 'qp':
self.__qp(X, y, kernel, C)
elif solver == 'smo':
self.__smo(X, y, kernel, C, epochs)
def predict(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
y : shape (n_samples,)
Predicted class label per sample, 1 or -1
'''
return np.sign(self.score(X))
def score(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
y : shape (n_samples,)
Predicted score of class per sample.
'''
kernel = self.__kernel_func(X, self.__X_support, self.__sigma)
return (self.__a_support * self.__y_support).dot(kernel) + self.__bias