当前位置:网站首页>机器学习(三)多项式回归
机器学习(三)多项式回归
2022-08-11 07:38:00 【ViperL1】
一、原理
原型公式:
与多项式回归的区别:仅有一个自变量,次方数不同
适用情况:

属于线性回归的原因:线性并不是对于图线而言,是对于y是否能由自变量线性表达。
二、Python处理
从模型角度:多项式回归为非线性模型
①设置工作路径
②数据预处理
③多项式回归模型的建立和拟合
--用于参照的线性回归模型
from sklearn.Liner_model import LinearRegession
Lin_reg=LinearRegression() --线性回归对象
Lin_reg.fit(x,y) --拟合
--多项式回归模型
--矩阵转换
from sklearn.preprocessing import PoltnomialFeatures
poly_reg = PolnomialFeatures(degeree=2) --用于将自变量替换为其更高阶的矩阵(示例为2次)
x_poly = poly_reg.fit_transform(x) --转换
--模型构造
lin_reg_2 = LinearRegression()
Lin_reg_2.fit(x_poly,y)

--绘制模型
plt.sactter(x,y,color='red') --绘点
plt.plot(x,lin_reg.predict(x),color='blue') --线性回归图像
plt.title('真假?(线性模型)')
plt.xlabel('职位水平')
plt.ylabel('薪水')
--绘制模型
plt.sactter(x,y,color='red') --绘点
plt.plot(x,lin_reg_2.predict(poly_reg.fit_transform(x)),color='blue')
--多项式回归图像
plt.title('真假?(多项式模型)')
plt.xlabel('职位水平')
plt.ylabel('薪水')
模拟结果已经好了很多,但是拟合度还有待提高。可以通过升高多项式的次数来提升拟合度
poly_reg = PolnomialFeatures(degeree=4) --模型次数升高为4
由于x轴间距过大,导致图像不够平缓,可以通过缩小点间距使得图像更为平整
x_grid = np.arange(min(x),max(x),0.1) --最小值、最大值、步距
x_grid = x_grid.reshape(len(x_grid),1) --转换为矩阵
--绘制模型
plt.sactter(x,y,color='red') --绘点
plt.plot(x_grid,lin_reg_2.predict(poly_reg.fit_transform(x_grid)),color='blue')
--多项式回归图像
plt.title('真假?(多项式模型)')
plt.xlabel('职位水平')
plt.ylabel('薪水')
④进行预测
lin_reg.predict(6.5) --线性模型预测
--误差较大
lin_reg_2.predict(poly_reg.fit_transform(6.5))
--误差较小边栏推荐
- 1.1-回归
- 初级软件测试工程师笔试试题,你知道答案吗?
- One-hot in TF
- 1056 Sum of Combinations (15 points)
- My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
- 1071 Small Gamble (15 points)
- 动态代理学习
- 伦敦银规则有哪些?
- Pico neo3在Unity中的交互操作
- Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
猜你喜欢

Serverless + domain name can also build a personal blog? Really, and soon
2.1 - Gradient Descent

TF中使用softmax函数;

求职简历这样写,轻松搞定面试官

Distributed Lock-Redission - Cache Consistency Solution

1091 N-自守数 (15 分)

1046 punches (15 points)

tf中自减操作;tf.assign_sub()

1076 Wifi Password (15 points)

go-grpc TSL认证 解决 transport: authentication handshake failed: x509 certificate relies on ... ...
随机推荐
The easiest trick to support quick renaming of various files
美术2.4 UV原理基础
Pico neo3在Unity中的交互操作
Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
国密规范 SM2 SM3 SM4
6月各手机银行活跃用户较快增长,创半年新高
【Pytorch】nn.ReLU(inplace=True)
基于微信小程序的租房小程序
为什么会没有内存了呢
TF中使用softmax函数;
【43. 字符串相乘】
线程交替输出(你能想出几种方法)
查询跟踪快递单号物流,智能分析物流中转有延误的单号
查找最新人员工资和上上次人员工资的变动情况
Four startup modes of Activity
选择收银系统主要看哪些方面?
1051 复数乘法 (15 分)
JUC并发编程
结合均线分析k线图的基本知识
matplotlib