Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gr33ndata/3c6f9c25279bb50a5cdd to your computer and use it in GitHub Desktop.
Save gr33ndata/3c6f9c25279bb50a5cdd to your computer and use it in GitHub Desktop.
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
x = [[1],[4],[7],[13],[10]]
# Y1 = 10 + 6*x
y1 = [16, 34, 52, 88, 70]
# Y2 = x*x = x^2
y2 = [1, 16, 49, 169, 100]
# This will convert X into
# [[1,1], [4,16], [7,49], .. etc]
# Only 1st and 2nd degree, since degree=2
pf = PolynomialFeatures(degree=2)
poly_x = pf.fit_transform(x)
# Modelling X and Y1, for X=5, Y should be 40
genius_regression_model = LinearRegression()
genius_regression_model.fit(poly_x, y1)
# Don't forget to Polynomial Transform your data to be predicted too
print 'For x = 5, y1 =', genius_regression_model.predict(pf.fit_transform([5]))
# Modelling X and Y2, for X=5, Y should be 25
genius_regression_model = LinearRegression()
genius_regression_model.fit(poly_x, y2)
# Don't forget to Polynomial Transform your data to be predicted too
print 'For x = 5, y2 =', genius_regression_model.predict(pf.fit_transform([5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment