Python 用 numpy 拟合数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18767523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:46:18  来源:igfitidea点击:

fitting data with numpy

pythonnumpyregressioncurve-fittingdata-fitting

提问by ezitoc

Let me start by telling that what I get may not be what I expect and perhaps you can help me here. I have the following data:

让我首先告诉我我得到的可能不是我所期望的,也许你可以在这里帮助我。我有以下数据:

>>> x
array([ 3.08,  3.1 ,  3.12,  3.14,  3.16,  3.18,  3.2 ,  3.22,  3.24,
    3.26,  3.28,  3.3 ,  3.32,  3.34,  3.36,  3.38,  3.4 ,  3.42,
    3.44,  3.46,  3.48,  3.5 ,  3.52,  3.54,  3.56,  3.58,  3.6 ,
    3.62,  3.64,  3.66,  3.68])

>>> y
array([ 0.000857,  0.001182,  0.001619,  0.002113,  0.002702,  0.003351,
    0.004062,  0.004754,  0.00546 ,  0.006183,  0.006816,  0.007362,
    0.007844,  0.008207,  0.008474,  0.008541,  0.008539,  0.008445,
    0.008251,  0.007974,  0.007608,  0.007193,  0.006752,  0.006269,
    0.005799,  0.005302,  0.004822,  0.004339,  0.00391 ,  0.003481,
    0.003095])

Now, I want to fit these data with, say, a 4 degree polynomial. So I do:

现在,我想用 4 度多项式拟合这些数据。所以我这样做:

>>> coefs = np.polynomial.polynomial.polyfit(x, y, 4)
>>> ffit = np.poly1d(coefs)

Now I create a new grid for x values to evaluate the fitting function ffit:

现在我为 x 值创建一个新网格来评估拟合函数ffit

>>> x_new = np.linspace(x[0], x[-1], num=len(x)*10)

When I do all the plotting (data set and fitting curve) with the command:

当我使用以下命令进行所有绘图(数据集和拟合曲线)时:

>>> fig1 = plt.figure()                                                                                           
>>> ax1 = fig1.add_subplot(111)                                                                                   
>>> ax1.scatter(x, y, facecolors='None')                                                                     
>>> ax1.plot(x_new, ffit(x_new))                                                                     
>>> plt.show()

I get the following:

我得到以下信息:

fitting_data.pngfitting_data.png

拟合数据.png拟合数据.png

What I expect is the fitting function to fit correctly (at least near the maximum value of the data). What am I doing wrong?

我期望的是拟合函数正确拟合(至少接近数据的最大值)。我究竟做错了什么?

Thanks in advance.

提前致谢。

采纳答案by askewchan

Unfortunately, np.polynomial.polynomial.polyfitreturns the coefficients in the opposite order of that for np.polyfitand np.polyval(or, as you used np.poly1d). To illustrate:

不幸的是,np.polynomial.polynomial.polyfit返回系数的顺序与 fornp.polyfitnp.polyval(或,如您使用的那样np.poly1d)的顺序相反。为了显示:

In [40]: np.polynomial.polynomial.polyfit(x, y, 4)
Out[40]: 
array([  84.29340848, -100.53595376,   44.83281408,   -8.85931101,
          0.65459882])

In [41]: np.polyfit(x, y, 4)
Out[41]: 
array([   0.65459882,   -8.859311  ,   44.83281407, -100.53595375,
         84.29340846])

In general: np.polynomial.polynomial.polyfitreturns coefficients [A, B, C]to A + Bx + Cx^2 + ..., while np.polyfitreturns: ... + Ax^2 + Bx + C.

通常:np.polynomial.polynomial.polyfit将系数返回[A, B, C]A + Bx + Cx^2 + ...,而np.polyfit返回:... + Ax^2 + Bx + C

So if you want to use this combination of functions, you must reverse the order of coefficients, as in:

因此,如果要使用这种函数组合,则必须颠倒系数的顺序,如下所示:

ffit = np.polyval(coefs[::-1], x_new)

However, the documentationstates clearly to avoid np.polyfit, np.polyval, and np.poly1d, and instead to use only the new(er) package.

但是,文档明确指出要避免np.polyfit,np.polyvalnp.poly1d, 而是仅使用 new(er) 包。

You're safest to use only the polynomial package:

只使用 polynomial 包是最安全的:

import numpy.polynomial.polynomial as poly

coefs = poly.polyfit(x, y, 4)
ffit = poly.polyval(x_new, coefs)
plt.plot(x_new, ffit)

Or, to create the polynomial function:

或者,创建多项式函数:

ffit = poly.Polynomial(coefs)    # instead of np.poly1d
plt.plot(x_new, ffit(x_new))

fit and data plot

拟合和数据图

回答by Charles Harris

Note that you can use the Polynomial class directly to do the fitting and return a Polynomial instance.

请注意,您可以直接使用 Polynomial 类进行拟合并返回 Polynomial 实例。

from numpy.polynomial import Polynomial

p = Polynomial.fit(x, y, 4)
plt.plot(*p.linspace())

puses scaled and shifted x values for numerical stability. If you need the usual form of the coefficients, you will need to follow with

p使用缩放和移位的 x 值来保证数值稳定性。如果您需要系数的通常形式,则需要遵循

pnormal = p.convert(domain=(-1, 1))