Python基于numpy模块实现回归预测
作者:Iceberg_710815 时间:2022-11-26 19:25:32
代码如下
import numpy as np
from matplotlib import pyplot as plt
# 用numpy生成数据t ,y
t = np.arange(1,10,1)
y = 0.9 * t + np.sin(t)
model = np.polyfit(t, y ,deg=1) # np.polyfit是numpy提供的加分分析方法,deg=1,指定模型为1阶的,返回值model为获得的模型
t2 = np.arange(-2,12,0.5) # 再生成一个间隔为0.5的序列
ypredict = np.polyval(model, t2) # 由np.polyval预测y值序列
plt.plot(t, y, "o", t2, ypredict, 'x')
plt.show()
上面的一段代码利用numpy生成数据序列,并实现了1阶回归,并画出预测效果图,图形如下:
将代码改一下,实现2阶、3阶回归预测,只需要model = np.polyfit(t, y, deg =2)即可,同理3阶模型就把deg改为3即可。
2阶效果图和3阶效果图分别如下:
需要说明的是,并不是拟合的阶数越高,模型越好,本例使用2阶拟合效果比较好,如果使用3阶,会出现“过拟合”
来源:https://www.cnblogs.com/iceberg710815/p/12448879.html
标签:Python,numpy,模块,回归,预测
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql 8.0 错误The server requested authentication method unknown to the client解决方法
2024-01-13 01:41:25
PHP实现的微信公众号扫码模拟登录功能示例
2023-11-22 18:13:08
如果用JS得到字符串中出现次数最多的字母
2007-12-03 21:01:00
Python脚本实现一键自动整理办公文件
2022-01-02 16:36:03
![](https://img.aspxhome.com/file/2023/5/121505_0s.jpg)
详解python tcp编程
2021-04-30 00:12:46
![](https://img.aspxhome.com/file/2023/1/90941_0s.png)
fso怎样判断一个盘上是否有文件
2007-09-26 12:35:00
nodejs实现简单的gulp打包
2024-05-11 10:16:22
python中利用h5py模块读取h5文件中的主键方法
2022-07-26 12:02:36
three.js实现炫酷的全景3D重力感应
2024-04-25 13:12:37
![](https://img.aspxhome.com/file/2023/8/135538_0s.jpg)
Oracle10g 安装方法
2009-06-19 17:50:00
详解阿里云视频直播PHP-SDK接入教程
2023-11-21 02:19:17
![](https://img.aspxhome.com/file/2023/1/111351_0s.png)
MySQL Explain命令用于查看执行效果
2009-02-27 15:30:00
Python开发之基于模板匹配的信用卡数字识别功能
2021-08-09 22:30:27
![](https://img.aspxhome.com/file/2023/3/120273_0s.png)
详解Node.js如何开发命令行工具
2024-05-05 09:21:19
深入理解MySQL重做日志 redo log
2024-01-13 22:25:36
![](https://img.aspxhome.com/file/2023/6/91446_0s.jpg)
Windows下mysql 8.0.29 winx64安装配置方法图文教程
2024-01-15 14:07:10
![](https://img.aspxhome.com/file/2023/9/129879_0s.jpg)
js实现动态显示时间效果
2024-06-12 17:43:06
SQL Server中ISNULL函数介绍
2009-09-09 21:23:00
详解golang碎片整理之 fmt.Scan
2024-02-20 12:51:16
Django-migrate报错问题解决方案
2021-11-05 13:30:16