ma.
polyfit
最小二乘多项式拟合。
注解
这是旧的多项式API的一部分。从版本1.4开始,新的多项式API在 numpy.polynomial 首选。差异可以在摘要中找到 transition guide .
numpy.polynomial
拟合多项式 p(x) = p[0] * x**deg + ... + p[deg] 程度的 deg 指向 (x, y) . 返回系数向量 p 这样可以最大限度地减少顺序中的平方误差。 deg , deg-1 ,… 0 .
p(x) = p[0] * x**deg + ... + p[deg]
这个 Polynomial.fit 由于类方法在数值上更稳定,因此建议对新代码使用类方法。有关详细信息,请参阅方法的文档。
Polynomial.fit
m采样点的x坐标 (x[i], y[i]) .
(x[i], y[i])
采样点的Y坐标。通过传递一个每列包含一个数据集的二维数组,可以同时拟合多个共享相同X坐标的采样点数据集。
拟合多项式的次数
拟合的相对条件数。相对于最大奇异值,小于此值的奇异值将被忽略。默认值是len(x)*eps,其中eps是浮点类型的相对精度,在大多数情况下约为2e-16。
开关决定返回值的性质。当它为假(默认值)时,仅返回系数,当同时返回奇异值分解的真实诊断信息时。
应用于采样点Y坐标的权重。对于高斯不确定性,使用1/sigma(而不是1/sigma**2)。
如果给予与否 False ,不仅返回估计,还返回其协方差矩阵。默认情况下,协方差按chi2/dof缩放,其中dof=M-(deg+1),即,除非在相对意义上,否则假设权重是不可靠的,并且缩放所有内容以使得缩减的chi2是统一的。如果 cov='unscaled' ,与权重为1/sigma**2的情况相关,已知sigma是不确定度的可靠估计。
cov='unscaled'
多项式系数,最高次优先。如果 y 是二维的,系数为 k -数据集在 p[:,k] .
p[:,k]
仅当存在时 full =正确。残差是最小二乘拟合残差的平方和、按比例计算的范德蒙系数矩阵的有效秩、其奇异值和 rcond . 有关详细信息,请参阅 linalg.lstsq .
仅当存在时 full =假和 cov =真。多项式系数估计的协方差矩阵。这个矩阵的对角线是每个系数的方差估计。如果y是二维数组,那么 k -数据集在 V[:,:,k]
cov
V[:,:,k]
最小二乘拟合中系数矩阵的秩是不足的。只有在以下情况下才会发出警告: full =假。
可通过以下方式关闭警告:
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
参见
polyval
计算多项式值。
linalg.lstsq
计算最小二乘拟合。
scipy.interpolate.UnivariateSpline
计算样条曲线拟合。
笔记
x中的任何屏蔽值都以y传播,反之亦然。
该解使平方误差最小化。
System Message: WARNING/2 (e=\sum_j=0_k_p(x_j)-y_j_2)
latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Double subscript. <argument> \split@tag \begin {split}e=\sum _j=0_k_ p(x_j)-y_j_2\end {split} l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} ! Double subscript. <argument> ...{split}e=\sum _j=0_k_p(x_j)-y_j_ 2\end {split} l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} ! Double subscript. <argument> \split@tag \begin {split}e=\sum _j=0_k_ p(x_j)-y_j_2\end {split} l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} ! Double subscript. <argument> ...{split}e=\sum _j=0_k_p(x_j)-y_j_ 2\end {split} l.14 ...t}e=\sum_j=0_k_p(x_j)-y_j_2\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 552 bytes). Transcript written on math.log.
方程式中:
x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0] x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1] ... x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]
系数矩阵 p 是范德蒙矩阵。
polyfit 问题A RankWarning 当最小二乘拟合条件很差时。这意味着由于数值误差,最佳拟合的定义不明确。通过降低多项式次数或替换,可以改善结果。 x 通过 x - x ()这个 rcond 参数也可以设置为小于其默认值的值,但结果拟合可能是假的:包括小奇异值的贡献可以为结果添加数字噪声。
注意,当多项式的阶数较大或采样点的间隔中心差时,拟合多项式系数的条件就很差。在这些情况下,应始终检查配合质量。当多项式拟合不令人满意时,样条曲线可能是一个很好的选择。
工具书类
维基百科,“曲线拟合”,https://en.wikipedia.org/wiki/curve_fitting
维基百科,“多项式插值”,https://en.wikipedia.org/wiki/多项式插值
实例
>>> import warnings >>> x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) >>> y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0]) >>> z = np.polyfit(x, y, 3) >>> z array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254]) # may vary
使用方便 poly1d 处理多项式的对象:
>>> p = np.poly1d(z) >>> p(0.5) 0.6143849206349179 # may vary >>> p(3.5) -0.34732142857143039 # may vary >>> p(10) 22.579365079365115 # may vary
高阶多项式可能剧烈振荡:
>>> with warnings.catch_warnings(): ... warnings.simplefilter('ignore', np.RankWarning) ... p30 = np.poly1d(np.polyfit(x, y, 30)) ... >>> p30(4) -0.80000000000000204 # may vary >>> p30(5) -0.99999999999999445 # may vary >>> p30(4.5) -0.10547061179440398 # may vary
插图:
>>> import matplotlib.pyplot as plt >>> xp = np.linspace(-2, 6, 100) >>> _ = plt.plot(x, y, '.', xp, p(xp), '-', xp, p30(xp), '--') >>> plt.ylim(-2,2) (-2, 2) >>> plt.show()