Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does np.polyfit do and return?

Tags:

python

numpy

I went through the docs but I'm not able to interpret correctly

IN my code, I wanted to find a line that goes through 2 points(x1,y1), (x2,y2), so I've used np.polyfit((x1,x2),(y1,y2),1) since its a 1 degree polynomial(a straight line)

It returns me [ -1.04 727.2 ] Though my code (which is a much larger file) runs properly, and does what it is intended to do - i want to understand what this is returning

I'm assuming polyfit returns a line(curved, straight, whatever) that satisfies(goes through) the points given to it, so how can a line be represented with 2 points which it is returning?

like image 926
Ravi Avatar asked Feb 11 '26 20:02

Ravi


1 Answers

From the 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 your polynomial. Thus, in your case:

y = -1.04*x + 727.2

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

like image 75
viloflo Avatar answered Feb 14 '26 09:02

viloflo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!