-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression.py
145 lines (112 loc) · 3.91 KB
/
regression.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
141
142
143
144
145
import sklearn as skl
import numpy as np
import pandas as pd
from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
from matplotlib import pyplot as plt
from sklearn.kernel_ridge import KernelRidge
from sklearn.linear_model import BayesianRidge
class KernelEstimator(skl.base.BaseEstimator, skl.base.TransformerMixin):
"""docstring"""
def __init__(self, save_path=None):
super(KernelEstimator, self).__init__()
self.save_path = save_path
def fit(self, X, y):
X, y = check_X_y(X, y)
self.y_mean = np.mean(y)
y -= self.y_mean
Xt = np.transpose(X)
cov = np.dot(X, Xt)
alpha, _, _, _ = np.linalg.lstsq(cov, y)
self.coef_ = np.dot(Xt, alpha)
if self.save_path is not None:
plt.figure()
plt.hist(self.coef_[np.where(self.coef_ != 0)], bins=50,
normed=True)
plt.savefig(self.save_path + "KernelEstimatorCoef.png")
plt.close()
return self
def predict(self, X):
check_is_fitted(self, ["coef_", "y_mean"])
X = check_array(X)
prediction = np.dot(X, self.coef_) + self.y_mean
if self.save_path is not None:
plt.figure()
plt.plot(prediction, "o")
plt.savefig(self.save_path + "KernelEstimatorPrediction.png")
plt.close()
return prediction
def score(self, X, y, sample_weight=None):
scores = (self.predict(X) - y)**2 / len(y)
score = np.sum(scores)
if self.save_path is not None:
plt.figure()
plt.plot(scores, "o")
plt.savefig(self.save_path + "KernelEstimatorScore.png")
plt.close()
df = pd.DataFrame({"score": scores})
df.to_csv(self.save_path + "KernelEstimatorScore.csv")
return score
def set_save_path(self, save_path):
self.save_path = save_path
class SklearnRidge():
def __init__(self, save_path=None):
self.save_path = save_path
self.reg = None
def fit(self, X, y):
X, y = check_X_y(X, y)
reg = KernelRidge(alpha=1.0)
reg.fit(X, y)
self.reg = reg
return self
def predict(self, X):
check_is_fitted(self, ["reg"])
X = check_array(X)
prediction = self.reg.predict(X)
return prediction
# def score(self, X, y, sample_weight=None):
# scores = (self.predict(X) - y) ** 2 / len(y)
# score = np.sum(scores)
#
# if self.save_path is not None:
# plt.figure()
# plt.plot(scores, "o")
# plt.savefig(self.save_path + "KernelEstimatorScore.png")
# plt.close()
#
# df = pd.DataFrame({"score": scores})
# df.to_csv(self.save_path + "KernelEstimatorScore.csv")
#
# return score
def set_save_path(self, save_path):
self.save_path = save_path
class skBayesianRidge():
def __init__(self, save_path=None):
self.save_path = save_path
self.reg = None
def fit(self, X, y):
X, y = check_X_y(X, y)
reg = BayesianRidge(verbose=True)
reg.fit(X, y)
self.reg = reg
return self
def predict(self, X):
check_is_fitted(self, ["reg"])
X = check_array(X)
prediction = self.reg.predict(X)
return prediction
# def score(self, X, y, sample_weight=None):
# scores = (self.predict(X) - y) ** 2 / len(y)
# score = np.sum(scores)
#
# if self.save_path is not None:
# plt.figure()
# plt.plot(scores, "o")
# plt.savefig(self.save_path + "KernelEstimatorScore.png")
# plt.close()
#
# df = pd.DataFrame({"score": scores})
# df.to_csv(self.save_path + "KernelEstimatorScore.csv")
#
# return score
def set_save_path(self, save_path):
self.save_path = save_path