Numpy.polyfit gives an empty array of leftovers

I use numpy.polyfit to fit a 2nd order polynomial to a dataset

fit1, fit_err1, _, _, _ = np.polyfit(xint[:index_max], yint[:index_max], 2, full=True)

For some few examples of my data, the fit_err1 variable fit_err1 empty, although the fit was successful, i.e. fit1 not empty!

Does anyone know what empty residual funds are in this context? Thanks!

EDIT: one example dataset:

 x = [-488., -478., -473.] y = [ 0.02080881, 0.03233648, 0.03584448] fit1, fit_err1, _, _, _ = np.polyfit(x, y, 2, full=True) 

result:

 fit1 = [ -3.00778818e-05 -2.79024663e-02 -6.43272769e+00] fit_err1 = [] 

I know that fitting a 2nd order polynomial to a set of three points is not very useful, but then I still expect the function to either raise a warning, or (since it actually determined the match) return the actual residuals, or both (like "here leftovers, but your conditions are poor! ").

+5
source share
1 answer

As @Jaime pointed out, if you have three points, then the second-order polynomial will match it exactly. And your point of view that the error should be 0 rather than an empty array makes sense, but this is the current behavior of np.linalg.lstsq , where np.polyfit wrapped around .

We can test this behavior by performing the least squares equation y = a*x**0 + b*x**1 + c*x**2 , which, as we know, should be a=0, b=0, c=1 :

 np.linalg.lstsq([[1, 1 ,1], [1, 2, 4], [1, 3, 9]], [1, 4, 9]) #(array([ -3.43396424e-15, 3.88578059e-15, 1.00000000e+00]), # array([], dtype=float64), # 3, # array([ 10.64956309, 1.2507034 , 0.15015641])) 

where we see that the second output is an empty array. And this should work as follows.

+5
source

Source: https://habr.com/ru/post/1204933/


All Articles