sklearn.pipeline

polynominal regression 只在x样本的大小范围内有效。

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
"""
demo sklearn.pipeline
"""
import numpy as np
import matplotlib.pyplot as plt
import sklearn.linear_model as lm
import sklearn.preprocessing as sp
import sklearn.metrics as sm
import sklearn.pipeline as pl

# 采集数据
x, y = np.loadtxt(
'../ml_data/single.txt', delimiter=',',
usecols=(0, 1), unpack=True)

# 训练多项式回归模型
x = x.reshape(-1, 1)
model = pl.make_pipeline(
sp.PolynomialFeatures(8),
lm.LinearRegression())
# 训练模型
model.fit(x, y)


# 预测输出
pred_y = model.predict(x)
print(sm.r2_score(y, pred_y))
print(sm.r2_score(pred_y, y))
# 绘制多项式曲线
px = np.linspace(x.min(), x.max(), 1000)
pred_y = model.predict(px.reshape(-1, 1))

# 画图
plt.figure('Poly Regression', facecolor='lightgray')
plt.title('Poly Regression', fontsize=16)
plt.xlabel('X')
plt.ylabel('Y')
plt.tick_params(labelsize=10)
plt.grid(linestyle=':')
plt.scatter(x, y, color='dodgerblue', label='Samples', s=70, marker='o')
plt.plot(px, pred_y, color='orangered', label='Poly Line')
plt.legend()
plt.savefig('polynominal.png')
plt.show()