多项式回归代码及相关代码解释

from sklearn.preprocessing import PolynomialFeatures     
import numpy as np
from sklearn.linear_model import LinearRegression      
import matplotlib.pyplot as plt                    #事实告诉我们没有pyplot,就不可以用plt.show()等等

x = np.array([258,270,294,320,342,368,396,446,480,586])[:,np.newaxis]
y = np.array([236,234,252,298,314,342,360,368,391,390])

lr = LinearRegression()
pr = LinearRegression()

quadratic = PolynomialFeatures(degree=2)
x_quad = quadratic.fit_transform(x)

#为了比较,先拟合一个简单的线性回归模型
lr.fit(x,y)
x_fit = np.arange(250,600,10)[:,np.newaxis]       #三个参数的意思是250起点;600终点;10为步长
y_lin_fit = lr.predict(x_fit)

pr.fit(x_quad,y)                                       #使用变换后的下x_quad特征作为多项式回归参数拟合多元回归模型
y_quad_fit = pr.predict(quadratic.fit_transform(x_fit))

#绘制图像
plt.scatter(x,y,label = 'training points')
plt.plot(x_fit,y_lin_fit,label = 'linear fit',linestyle = '--')
plt.plot(x_fit,y_quad_fit,label = 'quadratic fit')
plt.legend(loc = 'upper left')
plt.show()

附上图片
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值