Skip to content

Instantly share code, notes, and snippets.

@nithyadurai87
Created February 11, 2019 09:54
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 nithyadurai87/b7d3bf7733b5d4a8d2c8b2d1b8dcb531 to your computer and use it in GitHub Desktop.
Save nithyadurai87/b7d3bf7733b5d4a8d2c8b2d1b8dcb531 to your computer and use it in GitHub Desktop.
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
X = pd.DataFrame([100,200,300,400,500,600],columns=['sqft'])
y = pd.DataFrame([543543,34543543,35435345,34534,34534534,345345],columns=['Price'])
lin = LinearRegression()
lin.fit(X, y)
plt.scatter(X, y, color = 'blue')
plt.plot(X, lin.predict(X), color = 'red')
plt.title('Linear Regression')
plt.xlabel('sqft')
plt.ylabel('Price')
plt.show()
for i in [2,3,4,5]:
poly = PolynomialFeatures(degree = i)
X_poly = poly.fit_transform(X)
poly.fit(X_poly, y)
lin2 = LinearRegression()
lin2.fit(X_poly, y)
plt.scatter(X, y, color = 'blue')
plt.plot(X, lin2.predict(poly.fit_transform(X)), color = 'red')
plt.title('Polynomial Regression')
plt.xlabel('sqft')
plt.ylabel('Price')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment