-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path295-多项式回归.py
56 lines (39 loc) · 1.38 KB
/
295-多项式回归.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
# 1、生成数据集
import numpy as np
import matplotlib.pyplot as plt
rnd = np.random.RandomState(24)
x = rnd.uniform(-3, 3, size=100)
y = np.sin(x) + rnd.normal(size=len(x)) / 3
# plt.scatter(x, y)
# plt.show()
# sklearn只接受二维数据,先转化
x = x.reshape(-1, 1)
# 2、使用原始数据
from sklearn.linear_model import LinearRegression
linear = LinearRegression()
linear.fit(x, y)
print(linear.score(x, y)) #0.500
# 创建连续变量进行预测
line = np.linspace(-3, 3, 100).reshape(-1,1)
line_pred = linear.predict(line)
# plt.plot(line, line_pred, c='red')
# plt.scatter(x, y)
# plt.show()
# R2分数0.5,拟合的是一条直线,看不出sin函数的变化趋势,效果很差。
# 3、多项式回归
from sklearn.preprocessing import PolynomialFeatures
# 创建3次项,保留交叉项(两个以上的特征时才有意义),不要截距项(线性回归模型中会添加)
poly = PolynomialFeatures(degree=3, interaction_only=False, include_bias=False)
x_ = poly.fit_transform(x)
# 再做线性拟合
linear = LinearRegression()
linear.fit(x_, y)
print(linear.score(x_, y))
# 创建连续变量
line = np.linspace(-3, 3, 100).reshape(-1, 1)
line_ = poly.fit_transform(line)
y_pred_ = linear.predict(line_)
plt.scatter(x, y)
plt.plot(line, y_pred_, c='red')
plt.show()
# 由图可见,将特征扩展到三次项后,基本能完全拟合出sin曲线了。