-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussian_svm.py
49 lines (31 loc) · 1.2 KB
/
gaussian_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
import numpy as np
import cvxpy as cp
from scipy.spatial.distance import pdist, squareform
from scipy.spatial import distance_matrix
class GaussianSVM:
def __init__(self, C, l):
self.C = C
self.l = l
def fit(self, X, y):
y = 2 * y - 1
if self.l == 'auto':
self.l = np.sqrt(0.5 * X.shape[1] * X.var())
self.X_train = X.copy()
n = X.shape[0]
K = np.exp(-0.5 * squareform(pdist(X) ** 2) / (self.l ** 2))
Y = np.diag(y.squeeze())
constraint_matrix = np.r_[Y, -Y]
constraint_vector = np.concatenate([self.C * np.ones(n), np.zeros(n)])
alpha = cp.Variable(n)
dual = cp.Problem(
cp.Minimize(0.5 * cp.quad_form(alpha, K) - y.T @ alpha),
[constraint_matrix @ alpha <= constraint_vector],
)
dual.solve()
self.alpha = alpha.value.reshape(-1, 1)
return self
def predict(self, X):
kx = np.exp(-0.5 * (distance_matrix(X, self.X_train) ** 2) / (self.l ** 2))
return kx @ self.alpha
def predict_class(self, X):
return ((1 + np.sign(self.predict(X))) / 2).astype(int)