Repository navigation
Expand file tree
/
Copy pathcompletesvr.py
More file actions
64 lines (44 loc) · 1.72 KB
/
Copy pathcompletesvr.py
File metadata and controls
64 lines (44 loc) · 1.72 KB
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
import numpy as np
from matplotlib import pyplot as plt
import scipy.optimize as so
class SVR():
def __init__(self, epsilon=0.5,kernel='linear',C=10000,gamma=1,degree=3):
self.epsilon = epsilon
self.kernel = {'poly':lambda x,y: np.dot(x, y.T)**degree,
'rbf':lambda x,y:np.exp(-gamma*np.abs((x-y)**2)),
'linear':lambda x,y: np.dot(x, y.T)}[kernel]
self.C = C
def _loss(self,alpha):
alpha=alpha.reshape(self.origAshape)
result=0
res=0
for i in range(len(alpha)):
res=res+np.dot(y[i],alpha[i])
for j in range(len(alpha)):
result=result+alpha[i]*alpha[j]*self.kernel(x[i],x[j])
result=-0.5*result-self.epsilon*np.sum(alpha)+res
return -result
def fit(self, X, y):
if len(X.shape) == 1:
X = X.reshape(-1, 1)
if len(y.shape) == 1:
y = y.reshape(-1, 1)
self.X = X
self.y = y
self.alpha=np.zeros_like(self.y, dtype=float)
self.origAshape=np.shape(self.alpha)
m, n = X.shape
C=1
bounds_alpha = so.Bounds(np.zeros(m), np.full(m, C))
result=so.minimize(self._loss,self.alpha,bounds=bounds_alpha)
self.alpha=result.x
print(self.alpha)
return self
def predict(self, X):
if len(X.shape) == 1:
X = X.reshape(-1, 1)
self.alpha=self.alpha.reshape(self.origAshape)
res=0
for i in range(len(self.alpha)):
res=res+self.alpha[i]*self.kernel(X[i],X)
return res + self.alpha[0]