Python – np.polyfit does what and returns?

np.polyfit does what and returns?… here is a solution to the problem.

np.polyfit does what and returns?

I went through the docs but couldn’t interpret it correctly

In my code, I want to find a straight line through 2 dots (x1,y1), (x2,y2), so I used it
np.polyfit((x1,x2),(y1,y2),1)
Because it is a 1-degree polynomial (a straight line).

It returns me [ -1.04 727.2 ]
While my code (which is a larger file) works fine and works as expected – I want to understand what it returns

I assume that polyfit returns a line (curved, straight, etc.) satisfying (passing) the point given to it, so how to represent a line with the 2 points it returns?

Solution

From numpy.polyfit documentation:

Returns:

p : ndarray, shape (deg + 1,) or (deg + 1, K)

Polynomial coefficients, highest power first. If y was 2-D, the coefficients for k-th data set are in p[:,k].

So these numbers are the coefficients of the polynomial. So, in your case:

y = -1.04*x + 727.2

By the way, if the polynomial has a degree of at least N-1, numpy.polyfit will only return an equation that passes through all points (assuming you have N points). Otherwise, it returns the best fit that minimizes the squared error.

Related Problems and Solutions