-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial_Regression.py
More file actions
36 lines (28 loc) · 1.1 KB
/
Polynomial_Regression.py
File metadata and controls
36 lines (28 loc) · 1.1 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
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn import linear_model
def poly_regression(x, y):
# x,y: numpy array
# x =x.reshape(-1,1)
poly = PolynomialFeatures(degree=3)
x = poly.fit_transform(x) # eg,x= [x1,x2] -> [1,x1,x2,x1x1,x1x2,x2x2]
clf = linear_model.LinearRegression(fit_intercept=False)
clf.fit(x, y)
regress_coefs = clf.coef_
# model = Pipeline([('poly', PolynomialFeatures(degree=3)),
# ('linear', LinearRegression(fit_intercept=False))])
# model = model.fit(x[:, np.newaxis], y)
# regress_coefs = model.named_steps['linear'].coef_
# regress_intercept = model.named_steps['linear'].intercept_
# print(clf.predict(x))
# print(y)
print('残差平方和: %.2f' % np.mean((clf.predict(x) - y) ** 2))
return regress_coefs
# x = np.arange(5)
# y = 3 - 2 * x + x ** 2 - x ** 3
if __name__ == '__main__':
x = np.arange(20).reshape(10,2)
y = 3 - 2 * x[:,0] + x[:,1]
print(poly_regression(x, y))