没有合适的资源?快使用搜索试试~ 我知道了~
python多项式拟合之np.polyfit 和 np.polyld详解
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
15 下载量 12 浏览量
2020-09-17
22:01:57
上传
评论
收藏 46KB PDF 举报
温馨提示
试读
2页
主要介绍了python多项式拟合之np.polyfit 和 np.polyld的实例代码,python数据拟合主要可采用numpy库,库的安装可直接用pip install numpy等,需要的朋友跟随小编一起学习吧
资源推荐
资源详情
资源评论
python多项式拟合之多项式拟合之np.polyfit 和和 np.polyld详解详解
主要介绍了python多项式拟合之np.polyfit 和 np.polyld的实例代码,python数据拟合主要可采用numpy库,库的
安装可直接用pip install numpy等,需要的朋友跟随小编一起学习吧
python数据拟合主要可采用numpy库,库的安装可直接用pip install numpy等。
1. 原始数据:假如要拟合的数据yyy来自sin函数,np.sin
import numpy as np
import matplotlib.pyplot as plt
xxx = np.arange(0, 1000) # x值,此时表示弧度
yyy = np.sin(xxx*np.pi/180) #函数值,转化成度
2. 测试不同阶的多项式,例如7阶多项式拟合,使用np.polyfit拟合,np.polyld得到多项式系数
z1 = np.polyfit(xxx, yyy, 7) # 用7次多项式拟合,可改变多项式阶数;
p1 = np.poly1d(z1) #得到多项式系数,按照阶数从高到低排列
print(p1) #显示多项式
3. 求对应xxx的各项拟合函数值
yvals=p1(xxx) # 可直接使用yvals=np.polyval(z1,xxx)
4. 绘图如下
plt.plot(xxx, yyy, '*',label='original values')
plt.plot(xxx, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4) # 指定legend在图中的位置,类似象限的位置
plt.title('polyfitting')
plt.show()
5. np.polyfit函数:采用的是最小二次拟合,numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False),前三个参
数是必须的
官方文档:https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.polyfit.html
6. np.polyld函数:得到多项式系数,主要有三个参数
A one-dimensional polynomial class.
A convenience class, used to encapsulate "natural" operations on
polynomials so that said operations may take on their customary
form in code (see Examples).
Parameters
----------
c_or_r : array_like
The polynomial's coefficients, in decreasing powers, or if
the value of the second parameter is True, the polynomial's
roots (values where the polynomial evaluates to 0). For example,
``poly1d([1, 2, 3])`` returns an object that represents
:math:`x^2 + 2x + 3`, whereas ``poly1d([1, 2, 3], True)`` returns
one that represents :math:`(x-1)(x-2)(x-3) = x^3 - 6x^2 + 11x -6`.
r : bool, optional
If True, `c_or_r` specifies the polynomial's roots; the default
is False.
variable : str, optional
Changes the variable used when printing `p` from `x` to `variable`
(see Examples).
参数1表示:在没有参数2(也就是参数2默认False时),参数1是一个数组形式,且表示从高到低的多项式系数项,例如参数
1为[4,5,6]表示:
参数2表示:为True时,表示将参数1中的参数作为根来形成多项式,即参数1为[4,5,6]时表示:(x-4)(x-5)(x-6)=0,也就是:
参数3表示:换参数标识,用惯了x,可以用 t,s之类的
用法:用法:
1. 直接进行运算,例如多项式的平方,分别得到
资源评论
weixin_38545485
- 粉丝: 5
- 资源: 984
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功