numpy.polyfit with adapted parameters

2019-05-07 04:22发布

问题:

Regarding to this: polynomial equation parameters where I get 3 parameters for a squared function y = a*x² + b*x + c now I want only to get the first parameter for a squared function which describes my function y = a*x². With other words: I want to set b=c=0 and get the adapted parameter for a. In case I understand it right, polyfit isn't able to do this.

回答1:

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:



回答2:

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.])