numpy.polyfit with adapted parameters

喜你入骨 提交于 2019-12-05 13:38:14
Bas Swinckels

This can be done by numpy.linalg.lstsq. To explain how to use it, it is maybe easiest to show how you would do a standard 2nd order polyfit 'by hand'. Assuming you have your measurement vectors x and y, you first construct a so-called design matrix M like so:

M = np.column_stack((x**2, x, np.ones_like(x)))

after which you can obtain the usual coefficients as the least-square solution to the equation M * k = y using lstsq like this:

k, _, _, _ = np.linalg.lstsq(M, y)

where k is the column vector [a, b, c] with the usual coefficients. Note that lstsq returns some other parameters, which you can ignore. This is a very powerful trick, which allows you to fit y to any linear combination of the columns you put into your design matrix. It can be used e.g. for 2D fits of the type z = a * x + b * y (see e.g. this example, where I used the same trick in Matlab), or polyfits with missing coefficients like in your problem.

In your case, the design matrix is simply a single column containing x**2. Quick example:

import numpy as np
import matplotlib.pylab as plt

# generate some noisy data
x = np.arange(1000)
y = 0.0001234 * x**2 + 3*np.random.randn(len(x))

# do fit
M = np.column_stack((x**2,)) # construct design matrix
k, _, _, _ = np.linalg.lstsq(M, y) # least-square fit of M * k = y

# quick plot
plt.plot(x, y, '.', x, k*x**2, 'r', linewidth=3)
plt.legend(('measurement', 'fit'), loc=2)
plt.title('best fit: y = {:.8f} * x**2'.format(k[0]))
plt.show()

Result:

The coefficients are get to minimize the squared error, you don't assign them. However, you can set some of the coefficients to zero if they are too much insignificant. E.g., I have a list of points on curve y = 33*x²:

In [51]: x=np.arange(20)

In [52]: y=33*x**2  #y = 33*x²

In [53]: coeffs=np.polyfit(x, y, 2)

In [54]: coeffs
Out[54]: array([  3.30000000e+01,   8.99625199e-14,  -7.62430619e-13])

In [55]: epsilon=np.finfo(np.float32).eps

In [56]: coeffs[np.abs(coeffs)<epsilon]=0

In [57]: coeffs
Out[57]: array([ 33.,   0.,   0.])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!