-
Notifications
You must be signed in to change notification settings - Fork 0
/
non_lin_regression.py
313 lines (210 loc) · 7.56 KB
/
non_lin_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from numpy.polynomial.polynomial import polyfit
from sklearn.metrics import mean_squared_error, r2_score
# USEFUL LINKS
# https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html
# ============================ pre-processing dataset ============================
X, Y = load_diabetes(return_X_y = True)
_feat_vars = {}
# calculating variance across features
for _kth in range(0, X.shape[1]):
_ = [pow(sub[_kth], 2) for sub in X]
_feat_vars[_kth] = np.std(_)
_1st = -1
_1key = 0
_2nd = -1
_2key = 0
_3rd = -1
_3key = 0
for key, val in _feat_vars.items():
if val > _1st:
_1st = val
_1key = key
elif val > _2nd:
_2nd = val
_2key = key
elif val > _3rd:
_3rd = val
_3key = key
else:
pass
X_reduced = []
# using feature with top 3 biggest variances
for _kth in [_1key, _2key, _3key]:
X_reduced.append([sub[_kth] for sub in X])
X_reduced = np.array(X_reduced).T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [sub[0] for sub in X_reduced]
y = [sub[1] for sub in X_reduced]
z = [sub[2] for sub in X_reduced]
color_map = plt.get_cmap('spring')
Y = Y/np.max(Y)
scatter_plot = ax.scatter(x, y, z, c = Y, cmap = color_map)
plt.colorbar(scatter_plot, label = 'target value')
plt.title('top 3 highest var features')
plt.show()
plt.close()
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.05, random_state = 1)
# ============================ linear regression ============================
svr_lin = SVR(kernel = "linear", C = 100, gamma = "auto")
svr_lin.fit(X_train, Y_train)
y_predicted = []
y_expected = []
for i in range(0, len(X_test)):
pred = svr_lin.predict([X_test[i]])
y_predicted.append(np.round(pred[0], 3))
y_expected.append(np.round(Y_test[i], 3))
errors = []
_mse = 0
for i in range(len(y_expected)):
err = (y_expected[i] - y_predicted[i])**2
errors.append(err)
_mse += err
print('linear - MSE: ', np.round(_mse/len(y_expected), 3))
print("Mean squared error: %.2f" % mean_squared_error(y_expected, y_predicted))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f\n" % r2_score(y_expected, y_predicted))
plt.title('linear SVR')
# plt.yticks(np.arange(0.0, 0.6, step = 0.05))
# plt.ylim(0.0, 0.5)
plt.yticks(np.arange(50, 400, step = 50))
plt.xticks(np.arange(50, 400, step = 50))
# plt.plot(errors)
# plt.xlabel('Predicted Value')
# plt.ylabel('Mean Squared Error')
b, m = polyfit(y_predicted, y_expected, 1)
plt.scatter(y_predicted, y_expected)
plt.plot(np.array(y_predicted), b + m * np.array(y_predicted), '-', color = 'k')
plt.show()
# ============================ non-linear regression 1 ============================
svr_poly = SVR(kernel = "poly", C = 100, gamma = "auto", degree = 3, epsilon = 0.1, coef0 = 1)
svr_poly.fit(X_train, Y_train)
y_predicted = []
y_expected = []
for i in range(0, len(X_test)):
pred = svr_poly.predict([X_test[i]])
y_predicted.append(np.round(pred[0], 3))
y_expected.append(np.round(Y_test[i], 3))
errors = []
_mse = 0
for i in range(len(y_expected)):
err = (y_expected[i] - y_predicted[i])**2
errors.append(err)
_mse += err
print('poly - MSE: ', np.round(_mse/len(y_expected), 3))
print("Mean squared error: %.2f" % mean_squared_error(y_expected, y_predicted))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f\n" % r2_score(y_expected, y_predicted))
plt.title('poly SVR')
# plt.yticks(np.arange(0.0, 0.6, step = 0.05))
# plt.ylim(0.0, 0.5)
plt.yticks(np.arange(50, 400, step = 50))
plt.xticks(np.arange(50, 400, step = 50))
# plt.plot(errors)
# plt.xlabel('Predicted Value')
# plt.ylabel('Mean Squared Error')
b, m = polyfit(y_predicted, y_expected, 1)
plt.scatter(y_predicted, y_expected)
plt.plot(np.array(y_predicted), b + m * np.array(y_predicted), '-', color = 'k')
plt.show()
# ============================ non-linear regression 2 ============================
svr_rbf = SVR(kernel = "rbf", C = 100, gamma = 0.1, epsilon = 0.1)
svr_rbf.fit(X_train, Y_train)
y_predicted = []
y_expected = []
for i in range(0, len(X_test)):
pred = svr_rbf.predict([X_test[i]])
y_predicted.append(np.round(pred[0], 3))
y_expected.append(np.round(Y_test[i], 3))
errors = []
_mse = 0
for i in range(len(y_expected)):
err = (y_expected[i] - y_predicted[i])**2
errors.append(err)
_mse += err
print('rbf - MSE: ', np.round(_mse/len(y_expected), 3))
print("Mean squared error: %.2f" % mean_squared_error(y_expected, y_predicted))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f\n" % r2_score(y_expected, y_predicted))
plt.title('rbf SVR')
# plt.yticks(np.arange(0.0, 0.6, step = 0.05))
# plt.ylim(0.0, 0.5)
plt.yticks(np.arange(50, 400, step = 50))
plt.xticks(np.arange(50, 400, step = 50))
# plt.plot(errors)
# plt.xlabel('Predicted Value')
# plt.ylabel('Mean Squared Error')
b, m = polyfit(y_predicted, y_expected, 1)
plt.scatter(y_predicted, y_expected)
plt.plot(np.array(y_predicted), b + m * np.array(y_predicted), '-', color = 'k')
plt.show()
# ============================ non-linear regression 3 ============================
DTR = DecisionTreeRegressor(random_state = 0)
DTR.fit(X_train, Y_train)
y_predicted = []
y_expected = []
for i in range(0, len(X_test)):
pred = DTR.predict([X_test[i]])
y_predicted.append(np.round(pred[0], 3))
y_expected.append(np.round(Y_test[i], 3))
errors = []
_mse = 0
for i in range(len(y_expected)):
err = (y_expected[i] - y_predicted[i])**2
errors.append(err)
_mse += err
print('DTR - MSE: ', np.round(_mse/len(y_expected), 3))
print("Mean squared error: %.2f" % mean_squared_error(y_expected, y_predicted))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f\n" % r2_score(y_expected, y_predicted))
plt.title('DTR')
# plt.yticks(np.arange(0.0, 0.6, step = 0.05))
# plt.ylim(0.0, 0.5)
plt.yticks(np.arange(50, 400, step = 50))
plt.xticks(np.arange(50, 400, step = 50))
# plt.plot(errors)
# plt.xlabel('Predicted Value')
# plt.ylabel('Mean Squared Error')
b, m = polyfit(y_predicted, y_expected, 1)
plt.scatter(y_predicted, y_expected)
plt.plot(np.array(y_predicted), b + m * np.array(y_predicted), '-', color = 'k')
plt.show()
# ============================ linear regression 2 ============================
linreg = LinearRegression()
linreg.fit(X_train, Y_train)
y_predicted = []
y_expected = []
for i in range(0, len(X_test)):
pred = linreg.predict([X_test[i]])
y_predicted.append(np.round(pred[0], 3))
y_expected.append(np.round(Y_test[i], 3))
errors = []
_mse = 0
for i in range(len(y_expected)):
err = (y_expected[i] - y_predicted[i])**2
errors.append(err)
_mse += err
print('linreg - MSE: ', np.round(_mse/len(y_expected), 3))
print("Mean squared error: %.2f" % mean_squared_error(y_expected, y_predicted))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f\n" % r2_score(y_expected, y_predicted))
plt.title('linreg')
# plt.yticks(np.arange(0.0, 0.6, step = 0.05))
# plt.ylim(0.0, 0.5)
plt.yticks(np.arange(50, 400, step = 50))
plt.xticks(np.arange(50, 400, step = 50))
# plt.plot(errors)
# plt.xlabel('Predicted Value')
# plt.ylabel('Mean Squared Error')
b, m = polyfit(y_predicted, y_expected, 1)
plt.scatter(y_predicted, y_expected)
plt.plot(np.array(y_predicted), b + m * np.array(y_predicted), '-', color = 'k')
plt.show()