Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created November 18, 2020 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/abca36b5e061ccdfd0f24dd2a77698d2 to your computer and use it in GitHub Desktop.
Save yuyasugano/abca36b5e061ccdfd0f24dd2a77698d2 to your computer and use it in GitHub Desktop.
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