pythonnumpy

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?


Solution

  • 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,