user2738698
user2738698

Reputation: 578

How does numpy.polyfit return a slope and y-intercept when its documentation says it returns otherwise?

I have seen examples where slope, yintercept = numpy.polyfit(x,y,1) is used to return slope and y-intercept, but the documentation does not mention "slope" or "intercept" anywhere.

The documentation also states three different return options:

or

or

I have also run code with exactly the previous line and the slope and yintercept variables are filled with a single value each, which does not fit any of the documented returns. Which documentation am I supposed to use, or what am I missing?

Upvotes: 2

Views: 343

Answers (1)

mkrieger1
mkrieger1

Reputation: 23235

slope, yintercept = numpy.polyfit(x, y, 1) 

Both full and cov parameters are False, so it's the first option, "Polynomial coefficients, highest power first". deg is 1, so "p: ndarray, shape (deg + 1,)" is an array of shape (2,).

With tuple assignment,

  • slope is p[0], the xdeg-0 = x1 coefficient,
  • yintercept is p[1], the xdeg-1 = x0 coefficient.

Upvotes: 1

Related Questions