I am figuring out how to use the np.polyfit function and the documentation confuses me. In particular, I am trying to perform linear regression and print related statistics like the sum of squared errors (SSE). Can someone provide clear and concise explanations, possibly with a minimal working example?
How to perform linear regression with numpy.polyfit and print error statistics?
5.9k views Asked by funkymickey At
1
np.polyfitreturns a tuple containing the coefficients parametrizing the best-fitting polynomial of degreedeg. To fit a line, usedeg = 1. You can return the residual (sum of squared errors) by passingfull = Trueas an argument topolyfit. Note that with this argument,polyfitwill also return some other information about the fit, which we can just discard.Altogether, then, we have might have something like
The
*_notation in the call topolyfitjust tells Python to discard however many additional values are returned by the function. The documentation can tell you about these extra values if you're interested. We have to parse the SSE as a tuple(SSE,)becausepolyfitreturns it as a singleton array. This code produces something like this plot.You might also like to know about
np.polyval, which will take tuples of polynomial coefficients and evaluate the corresponding function at input points.