regression - How to get the sum of least squares/error from polyfit in one dimension Python -
i want linear regression scatter plot using polyfit, , want residual see how linear regression is. unsure how isn't possible residual output value polyfit since 1 dimensional. code:
p = np.polyfit(lengths, breadths, 1) m = p[0] b = p[1] yfit = np.polyval(p,lengths) newlengths = [] y in lengths: newlengths.append(y*m+b) ax.plot(lengths, newlengths, '-', color="#2c3e50")
i saw stackoverflow answer used polyval - unsure of gives me. exact values lengths? should find error finding delta of each element polyval , 'breadth'?
you can use keyword full=true
when calling polyfit
(see http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html) least-square error of fit:
coefs, residual, _, _, _ = np.polyfit(lengths, breadths, 1, full=true)
you can same answer doing:
coefs = np.polyfit(lengths, breadths, 1) yfit = np.polyval(coefs,lengths) residual = np.sum((breadths-yfit)**2)
or
residual = np.std(breadths-yfit)**2 * len(breadths)
additionally, if want plot residuals, can do:
coefs = np.polyfit(lengths, breadths, 1) yfit = np.polyval(coefs,lengths) plot(lengths, breadths-yfit)
Comments
Post a Comment