Skip to content

Instantly share code, notes, and snippets.

@regivm
Last active April 21, 2020 12:01
Show Gist options
  • Save regivm/b6f9ded5929af9d8490b2506744716df to your computer and use it in GitHub Desktop.
Save regivm/b6f9ded5929af9d8490b2506744716df to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from scipy.optimize import minimize
boston = datasets.load_boston()
y = boston.target
X = pd.DataFrame(boston.data, columns = boston.feature_names)
ss = StandardScaler()
Xs = ss.fit_transform(X)
Xs = pd.DataFrame(Xs, columns=boston.feature_names)
Xs['const'] = 1
Xss = Xs.copy()
lr = LinearRegression(fit_intercept=False)
lr.fit(Xss, y)
pd.DataFrame(list(lr.coef_), index=Xs.columns, columns=['coefs'])
y_pred = lr.predict(Xss)
print('Means Squared Error: ', mean_squared_error(y, y_pred))
print('R-Square: ', r2_score(y, y_pred))
plt.scatter(lr.predict(Xss), y, color = "red")
plt.scatter(lr.predict(Xss), lr.predict(Xss), color = "green")
plt.title("Predicted vs Actual Price")
plt.xlabel("Boston House Price(Predicted)")
plt.ylabel("House Price")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment