Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scikit-learn LinearRegression vs Numpy Polyfit
fx = np.linspace(min(x), max(x), 100) # x-axis data points
f1 = np.polyfit(x, y, 1)
f2 = np.polyfit(x, y, 2)
f3 = np.polyfit(x, y, 3)
f4 = np.polyfit(x, y, 4)
for coefs in [f1, f2, f3, f4]:
f = np.poly1d(coefs)
print('Coefficients: {}'.format(coefs[:-1]))
print('Intercept: {}'.format(coefs[-1]))
print('Error: {}'.format(error(f, x, y)))
fitted_curve = np.poly1d(coefs)(fx)
plt.scatter(x, y, label="observed")
plt.plot(fx, fitted_curve, c="red", label="fitted")
plt.grid()
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment