numpy.polynomial.polynomial.polyfit

polynomial.polynomial.polyfit(x, y, deg, rcond=None, full=False, w=None)[源代码]

多项式与数据的最小二乘拟合。

返回多项式的系数 deg 这是符合数据值的最小二乘法 y 给定点 x .如果 y 是1-d,返回系数也将是1-d。如果 y 是否进行了二维多次拟合,每列一次 y ,生成的系数存储在二维返回的相应列中。拟合多项式的形式为

System Message: WARNING/2 (P(x)=C_0+C_1 * x + ... + c_n * X^ n)

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) ! 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 ...=C_0+C_1 * x + ... + c_n * X^ n\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 ...=C_0+C_1 * x + ... + c_n * X^ n\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 ...=C_0+C_1 * x + ... + c_n * X^ n\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 ...=C_0+C_1 * x + ... + c_n * X^ n\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 496 bytes). Transcript written on math.log.

在哪里? ndeg .

参数
x :数组,形状 (M ,)阵列,形状(

X坐标 M 样本(数据)点 (x[i], y[i]) .

y :数组,形状 (M ,或) (MK阵列,形状(

采样点的Y坐标。多组共享相同X坐标的采样点可以(独立)与一个调用匹配 polyfit 通过传递 y 每列包含一个数据集的二维数组。

degint或一维数组

拟合多项式的度数。如果 deg 是一个整数,所有项都小于或等于 deg “这一术语包含在拟合中。对于numpy版本>=1.11.0,可以使用指定要包含项的度数的整数列表。

rcond可选浮动

拟合的相对条件数。奇异值小于 rcond 相对于最大奇异值,将被忽略。默认值为 len(x)*eps 在哪里 eps 是平台浮动类型的相对精度,在大多数情况下约为2E-16。

full可选的布尔

开关确定返回值的性质。什么时候? False (默认值)只返回系数;当 True 同时返回奇异值分解(用于求解拟合矩阵方程)的诊断信息。

w :数组,形状 (M ,),可选阵列,形状(

砝码。如果不是没有,每个点的贡献 (x[i],y[i]) 拟合的权重为 w[i] . 理想情况下,选择权重是为了使产品的误差 w[i]*y[i] 都有相同的差异。默认值为无。

1.5.0 新版功能.

返回
coef :ndarray,形状 (deg + 1) (deg + 1, K排列,形状(

多项式系数由低到高排序。如果 y 是二维的,列中的系数 k 属于 coef 表示符合数据的多项式 yk 第四列。

[[residuals, rank, singular_values, rcond]]列表

只有当 full =真

resid——最小二乘拟合秩的平方残差之和——标度范德蒙矩阵Sv的数值秩——标度范德蒙矩阵Rcond的奇异值——标度范德蒙矩阵Rcond的值。 rcond .

有关详细信息,请参阅 numpy.linalg.lstsq .

加薪
RankWarning

如果最小二乘拟合中的矩阵是秩亏的,则引发。只有在以下情况下才会发出警告: full =假。警告可通过以下方式关闭:

>>> import warnings
>>> warnings.simplefilter('ignore', np.RankWarning)

笔记

解是多项式的系数。 p 使加权平方误差之和最小化

System Message: WARNING/2 (E=\sum j w_j^2*y_j-p(x_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) ! 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 ...sum j w_j^2*y_j-p(x_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 ...sum j w_j^2*y_j-p(x_j)^2,\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...sum j w_j^2*y_j-p(x_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 ...sum j w_j^2*y_j-p(x_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 ...sum j w_j^2*y_j-p(x_j)^2,\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...sum j w_j^2*y_j-p(x_j)^2,\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 548 bytes). Transcript written on math.log.

何处 w_j 是重量。通过建立(通常)超定矩阵方程来解决这个问题:

System Message: WARNING/2 (V(x) * c = w * Y)

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) ! 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 \begin{split}V(x) * c = w * Y\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 \begin{split}V(x) * c = w * Y\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 \begin{split}V(x) * c = w * Y\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 \begin{split}V(x) * c = w * Y\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 356 bytes). Transcript written on math.log.

在哪里? V 是的加权伪范德蒙矩阵 xc 是要求解的系数, w 是重量,和 y 是观察值。然后用奇异值分解法求解该方程。 V .

如果某些奇异值 V 太小以致被忽视(和 full = False a) RankWarning 将被提升。这意味着系数值的确定可能很差。拟合到低阶多项式通常会消除警告(当然,可能不是您想要的;如果您有独立的理由选择不起作用的程度,则可能必须:a)重新考虑这些原因,和/或b)重新考虑数据的质量)。这个 rcond 也可以将参数设置为小于其默认值的值,但由此产生的拟合可能是虚假的,并且具有来自舍入误差的较大贡献。

使用双精度的多项式拟合往往在(多项式)阶数20时“失败”。使用切比雪夫或勒让德级数的拟合通常条件较好,但很大程度上仍然取决于采样点的分布和数据的平滑度。如果配合质量不够,样条曲线可能是一个很好的选择。

实例

>>> np.random.seed(123)
>>> from numpy.polynomial import polynomial as P
>>> x = np.linspace(-1,1,51) # x "data": [-1, -0.96, ..., 0.96, 1]
>>> y = x**3 - x + np.random.randn(len(x)) # x^3 - x + N(0,1) "noise"
>>> c, stats = P.polyfit(x,y,3,full=True)
>>> np.random.seed(123)
>>> c # c[0], c[2] should be approx. 0, c[1] approx. -1, c[3] approx. 1
array([ 0.01909725, -1.30598256, -0.00577963,  1.02644286]) # may vary
>>> stats # note the large SSR, explaining the rather poor results
 [array([ 38.06116253]), 4, array([ 1.38446749,  1.32119158,  0.50443316, # may vary
          0.28853036]), 1.1324274851176597e-014]

同样的东西没有额外的噪音

>>> y = x**3 - x
>>> c, stats = P.polyfit(x,y,3,full=True)
>>> c # c[0], c[2] should be "very close to 0", c[1] ~= -1, c[3] ~= 1
array([-6.36925336e-18, -1.00000000e+00, -4.08053781e-16,  1.00000000e+00])
>>> stats # note the minuscule SSR
[array([  7.46346754e-31]), 4, array([ 1.38446749,  1.32119158, # may vary
           0.50443316,  0.28853036]), 1.1324274851176597e-014]