-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlinear_regression_locally_weight.py
40 lines (34 loc) · 1.12 KB
/
linear_regression_locally_weight.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
import numpy as np
import kernel
class LinearRegressionLocallyWeight:
def fit(self, X, y, sigma):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
y : shape (n_samples,)
Target values
gamma : For RBF kernel
'''
self.__X = np.insert(X, 0, 1, axis=1)
self.__y = y
self.__sigma = sigma
def predict(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
y : shape (n_samples,)
Predicted value per sample.
'''
X = np.insert(X, 0, 1, axis=1)
n_samples, n_features = X.shape
W = np.zeros((n_samples, n_features))
for i in range(n_samples):
Weights = np.diag(kernel.gaussian_kernel(self.__X, X[i].reshape(1, -1), self.__sigma).ravel())
W[i] = (np.linalg.pinv(self.__X.T.dot(Weights).dot(self.__X)).dot(self.__X.T).dot(Weights).dot(self.__y)).ravel()
return np.sum(X * W, axis=1)