-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolyReg.py
102 lines (87 loc) · 3.89 KB
/
PolyReg.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
# Polynomial regression function
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import numpy as np
import time
from scipy import linalg
from sklearn.metrics import mean_squared_error
def PolyReg(x_train,y_train,x_test,y_test,degree,method):
# Polynomial regression
# Set the maximum degree of the polynomial to fit
n_degree=degree
start = time.perf_counter()
# Create polynomial features for the training and testing data
poly = PolynomialFeatures(degree)
x_poly_train = poly.fit_transform(x_train)
x_poly_test = poly.fit_transform(x_test)
if method == 'QR':
# Prepare decompositions
XX = x_poly_train.T @ x_poly_train
XY = x_poly_train.T @ y_train
Q, R = np.linalg.qr(XX)
RB = Q.T@XY
Bhat_QR = linalg.solve_triangular(R,RB,lower=False)
# Predictions
pred_train = x_poly_train @ Bhat_QR
pred_test = x_poly_test @ Bhat_QR
# Fit a linear regression model on the polynomial features
#LR = LinearRegression()
#LR.fit(x_poly_train, y_train)
# Predict on the training data and calculate the RMSE
#pred1 = LR.predict(x_poly_train)
Trr = mean_squared_error(y_train, pred_train)
# Predict on the testing data and calculate the RMSE
#pred2 = LR.predict(x_poly_test)
Tss = mean_squared_error(y_test, pred_test)
if method == 'LU':
# Create polynomial features for the training and testing data
# Prepare decompositions
XX = x_poly_train.T @ x_poly_train
XY = x_poly_train.T @ y_train
P, L, U = linalg.lu(XX)
LZ= P.T @ XY
Z = linalg.solve_triangular(L,LZ,lower=True)
Bhat_LU = linalg.solve_triangular(U,Z,lower=False)
# Predictions
pred_train = x_poly_train @ Bhat_LU
pred_test = x_poly_test @ Bhat_LU
# Fit a linear regression model on the polynomial features
#LR = LinearRegression()
#LR.fit(x_poly_train, y_train)
# Predict on the training data and calculate the RMSE
#pred1 = LR.predict(x_poly_train)
Trr = mean_squared_error(y_train, pred_train)
# Predict on the testing data and calculate the RMSE
#pred2 = LR.predict(x_poly_test)
Tss = mean_squared_error(y_test, pred_test)
# if method=='CHOLESKY':
# for i in range(2, n_degree):
# Create polynomial features for the training and testing data
# poly = PolynomialFeatures(degree=i)
# x_poly_train = poly.fit_transform(x_train)
# x_poly_test = poly.fit_transform(x_test)
# Prepare decompositions
# XX = x_poly_train.T @ x_poly_train
# XY = x_poly_train.T @ y_train
# print (np.linalg.eigvalsh(XX))
# print(np.linalg.eigvalsh(XY))
# c,low = scipy.linalg.cho_factor(XX)
# Bhat_cho = scipy.linalg.cho_solve((c,low),XY)
# print(Bhat_cho)
# Predictions
# pred_train = x_poly_train @ Bhat_cho
# pred_test = x_poly_test @ Bhat_cho
# print(pred_train.shape,pred_test.shape)
# Fit a linear regression model on the polynomial features
#LR = LinearRegression()
#LR.fit(x_poly_train, y_train)
# Predict on the training data and calculate the MSE
#pred1 = LR.predict(x_poly_train)
# Trr.append(mean_squared_error(y_train, pred_train))
# Predict on the testing data and calculate the RMSE
#pred2 = LR.predict(x_poly_test)
# Tss.append(mean_squared_error(y_test, pred_test))
end = time.perf_counter()
# Plot the MSE of the training and testing errors for different degrees of the polynomial
total = end - start
return (Trr, Tss, total)