当前位置:网站首页>机器学习实战-多项式回归结合Pipeline管道机制
机器学习实战-多项式回归结合Pipeline管道机制
2022-08-06 11:58:00 【叶小小qaq】
多项式回归
前面我们拟合直线用到了线性回归,而非线性回归中,则需要建立因变量和自变量之间的非线性关系。多项式回归模型是线性回归模型的一种,此时回归函数关于回归系数是线性的。由于任一函数都可以用多项式逼近,所以应用非常广泛。
公式

其中,m表示多项式的阶数, x j x^j xj 表示 x 的 j 次幂,w 则代表该多项式的系数。
构造数据集
# 构造数据集
import numpy as np
x = np.random.uniform(-3, 3, size=100)
X = x.reshape((-1, 1))
y = 0.5 * x**2 + x + 2 + np.random.normal(size=100)
使用Pipeline封装多个重复操作
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
# 使用Pipeline封装多个重复操作
pipeline = Pipeline([
("poly", PolynomialFeatures(degree=2)), # 多项式回归
("std_scaler", StandardScaler()), # 标准化
("lin_reg", LinearRegression()) # 线性回归
])
训练拟合模型进行预测
pipeline.fit(X, y)
y_predict = pipeline.predict(X)
绘图
import matplotlib.pyplot as plt
# 绘图
plt.scatter(x, y)
plt.plot(np.sort(x), y_predict[np.argsort(x)], color='r')
plt.show()

由上图可以看出得到的拟合模型是一条没有规律的曲线,随着PolynomialFeatures(degree=2))中参数的增大,拟合度会随之增加,但是达到一定的拟合度后,模型预测结果偏差会出现逐渐增大的现象,即过拟合现象
相关概念:
欠拟合:过于简单的模型,无论是训练数据还是测试数据都无法给出足够精度的现象;
过拟合:过于复杂的模型,对于训练数据具有很高的精度,但对于测试数据通常精度很低的现象;
全部代码
import numpy as np
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
# 构造数据集
x = np.random.uniform(-3, 3, size=100)
X = x.reshape((-1, 1))
y = 0.5 * x**2 + x + 2 + np.random.normal(size=100)
print(X.shape, y.shape)
# 使用Pipeline封装多个重复操作
pipeline = Pipeline([
("poly", PolynomialFeatures(degree=2)), # 多项式回归
("std_scaler", StandardScaler()), # 标准化
("lin_reg", LinearRegression()) # 线性回归
])
pipeline.fit(X, y)
y_predict = pipeline.predict(X)
# 绘图
plt.scatter(x, y)
plt.plot(np.sort(x), y_predict[np.argsort(x)], color='r')
plt.show()
博客园:https://www.cnblogs.com/yj179101536/
欢迎评论!
边栏推荐
- Can't verify the mobile phone number of registered Google account in 2022?The last step of Google account registration is stuck on this number can not be used for verification on the successful soluti
- Golang gin 配置腾讯云cos实现单文件与多文件上传
- How to use wireless serial communication module to realize long-distance communication between touch screen and PLC?
- JUC线程池(二): 一文搞定对线程池的疑问 - ThreadPoolExecutor详解
- PS6603-USB PD 协议 SINK 端输出控制器芯片
- d命名单元测试
- MySQL数据库安装步骤(图文)
- XML usage
- d find the function in the package
- 架构实战营模块九作业
猜你喜欢
随机推荐
stdout stderr 重定向到文件
P1747 What a strange game
kubernetes日常命令
d find the function in the package
PS6652+JD6606S_2C 35W双C口PD协议方案
Teach you to draw pixel art and share 195 issues every week
QT:使用自定义的信号与槽的方式
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
HCIP Day 15 Notes
d named unit test
NITZ time zone update air interface message
因宇航服存在安全问题 NASA叫停国际空间站所有太空行走任务
SQLNET.ORA中SQLNET.ALLOWED_LOGON_VERSION参数介绍
[Software Test Written Questions] Alibaba (China) Network Technology Co., Ltd.
太强了,一个注解搞定接口返回数据脱敏!
从ADVANCE.AI 全球产品负责人周洪丞的发言中了解其如何通过产品赋能中国出海企业
The digital transformation of how so difficult?!
Too strong, an annotation can desensitize the returned data of the interface!
ES6 new feature - generator
XML使用









