Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.polyfit gives empty residuals array

I use numpy.polyfit to fit a 2nd order polynom to a set of data

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

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

Does anybody know what an empty residual means in this context? Thank you!

EDIT: one example data set:

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 polynom to a set of three point is not very useful, but then i still expect the function to either raise a warning, or (as it actually determined a fit) return the actual residuals, or both (like "here are the residuals, but your conditions are poor!").

like image 542
jkalden Avatar asked Oct 17 '14 16:10

jkalden


People also ask

What does NP Polyfit return?

The np. polyfit() method takes a few parameters and returns a vector of coefficients p that minimizes the squared error in the order deg, deg-1, … 0. It least squares the polynomial fit.

What is Numpy Polyfit?

Introduction to NumPy polyfit. In python, Numpy polyfit() is a method that fits the data within a polynomial function. That is, it least squares the function polynomial fit. For example, a polynomial p(X) of deg degree fits the coordinate points (X, Y).

What is Rcond Polyfit?

rcond. The value of rcond is not really about quality of fit, it describes the process by which the fit was obtained, namely a least-squares solution of a linear system. Most of the time the user of polyfit does not provide this parameter, so a suitable value is picked by polyfit itself.

How do you fit a polynomial to data in Python?

To get the least-squares fit of a polynomial to data, use the polynomial. polyfit() in Python Numpy. The method returns the Polynomial coefficients ordered from low to high. If y was 2-D, the coefficients in column k of coef represent the polynomial fit to the data in y's k-th column.


1 Answers

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

We can test this behavior doing the least-squares fit of a y = a*x**0 + b*x**1 + c*x**2 equation that we know the answer 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 can see that the second output is an empty array. And this is intended to work like this.

like image 102
Saullo G. P. Castro Avatar answered Oct 10 '22 10:10

Saullo G. P. Castro