Ravi
Ravi

Reputation: 601

What does np.polyfit do and return?

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?

Upvotes: 3

Views: 12876

Answers (2)

atharva mulay
atharva mulay

Reputation: 1

These are essentially the beta and the alpha values for the given data. Where beta necessarily demonstrates the degree of volatility or the slope

Upvotes: 0

viloflo
viloflo

Reputation: 96

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.

Upvotes: 4

Related Questions