如何解决"polyfit可能条件不佳"?在numpy中? [英] How to fixed "polyfit maybe poorly conditioned" in numpy?

查看:94
本文介绍了如何解决"polyfit可能条件不佳"?在numpy中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy软件包对一组数据进行polyfit.

I am trying to do a polyfit on a set of data using numpy package.

以下是代码,它可以成功运行.当订单达到20(非常高)时,拟合线似乎适合数据.但是,最后,它说"Polyfit的状况可能不佳".

The following are the codes, it can run successfully. The fitted line seems to fit the data when the order reaches around 20(very high). However, at the end, it says "Polyfit may be poorly conditioned".

如果我没记错的话,难道是当度数越高时,拟合将对数据敏感,即容易受到数据的影响吗?我怎样才能解决这个问题?谢谢!

if i am not wrong, is it that when the degree gets higher, the fitting will be sensitive to the data, i.e. easily influenced by the data? How can I fix this? Thanks!

非常感谢您的回答.

def gen_data_9(length=5000):
x = 2.0 * (np.random.rand(length) - 0.5) * np.pi * 2.0
f = lambda x: np.exp(-x**2) * (-x) * 5 + x / 3
y = f(x) + np.random.randn(len(x)) * 0.5
return x, y, f

fig,ax = plt.subplots(3,3,figsize = (16,16))

for n in range(3):
    for k in range(3):

        order = 20*n+10*k+1
        z = np.polyfit(x,y,order)
        p = np.poly1d(z)

        ax[n,k].scatter(x,y,label = "Real data",s=1)
        ax[n,k].scatter(x,p(x),label = "Polynomial with order={}".format(order),
                    color='C1',s=1)
    ax[n,k].legend()

fig.show()

推荐答案

TL; DR:在这种情况下,警告表示:使用较低的顺序!

TL;DR: In this case the warning means: use a lower order!

要引用文档:

请注意,当多项式的阶数较大或采样点的间隔严重居中时,拟合多项式系数的内在条件很差.在这种情况下,应始终检查配合质量.如果多项式拟合不令人满意,则样条曲线可能是一个很好的选择.

Note that fitting polynomial coefficients is inherently badly conditioned when the degree of the polynomial is large or the interval of sample points is badly centered. The quality of the fit should always be checked in these cases. When polynomial fits are not satisfactory, splines may be a good alternative.

换句话说,警告告诉您仔细检查结果.如果它们看起来不错,请不要担心.但是他们还好吗?要回答这个问题,您不仅应该在用于拟合的数据点上评估结果的拟合度(这些数据点通常匹配得很好,尤其是在过度拟合时).考虑一下:

In other words, the warning tells you to double-check the results. If they seem fine don't worry. But are they fine? To answer that you should evaluate the resulting fit not only on the data points used for fitting (these often match rather well, especially when overfitting). Consider this:

xp = np.linspace(-1, 1, 10000) * 2 * np.pi

for n in range(3):
    for k in range(3):

        order = 20*n+10*k+1
        print(order)
        z = np.polyfit(x,y,order)
        p = np.poly1d(z)

        ax[n,k].scatter(x,y,label = "Real data",s=1)
        ax[n,k].plot(xp,p(xp),label = "Polynomial with order={}".format(order), color='C1')
        ax[n,k].legend()

在这里,我们在距离样本数据更精细的点上评估polyfit.结果是:

Here we evaluate the polyfit on points spaced much more finely than the sample data. This is the result:

您可以看到40个订单并取消了结果.这与我得到的警告相吻合.

You can see that for orders 40 and obove the results really shoot off. This coincides with the warnings I get.

这篇关于如何解决"polyfit可能条件不佳"?在numpy中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆