-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRidge.py
More file actions
69 lines (47 loc) · 2.01 KB
/
Copy pathRidge.py
File metadata and controls
69 lines (47 loc) · 2.01 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
64
65
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import import_ipynb
# In[2]:
import numpy as np
class Kernelridge():
def __init__(self, K, ID, eps=1e-5, lbda=0.1, solver=None):
self.K = K
self.ID = ID
self.eps = eps
self.lbda = lbda
self.solver = solver
def fit(self, X, y):
self.Id_fit = np.array(X.loc[:, 'Id'])
self.idx_fit = np.array([np.where(self.ID == self.Id_fit[i])[0] for i in range(len(self.Id_fit))]).squeeze()
self.K_fit = self.K[self.idx_fit][:, self.idx_fit]
self.y_fit, self.X_fit, = np.array(y.loc[:, 'Bound']), X
self.n = self.K_fit.shape[0]
self.a = np.dot(np.linalg.inv(self.K_fit + self.lbda * self.n * np.eye(self.n)), self.y_fit)
# Align support vectors index with index from fit set
self.idx_sv = np.where(np.abs(self.a) > self.eps)
self.y_fit = self.y_fit[self.idx_sv]
self.a = self.a[self.idx_sv]
self.idx_sv = self.idx_fit[self.idx_sv]
# Intercept
self.y_hat = np.array([np.dot(self.a, self.K[self.idx_sv, i]).squeeze() for i in self.idx_sv])
self.b = np.mean(self.y_fit - self.y_hat)
def predict(self, X):
# Align prediction IDs with index in kernel K
self.Id_pred = np.array(X.loc[:, 'Id'])
self.idx_pred = np.array([np.where(self.ID == self.Id_pred[i])[0] for i in range(len(self.Id_pred))]).squeeze()
pred = []
for i in self.idx_pred:
pred.append(np.sign(np.dot(self.a, self.K[self.idx_sv, i].squeeze()) + self.b))
return np.array(pred)
def score(self, pred, y):
"""
Compute accuracy of predictions according to y
:param pred: np.array, predictions (-1/1)
:param y: np.array or pd.DataFrame, true labels
:return: float, percentage of correct predictions
"""
label = np.array(y.loc[:, 'Bound']) if not isinstance(y, np.ndarray) else y
assert 0 not in np.unique(label),
return np.mean(pred == label)
# In[ ]: