Skip to content

Instantly share code, notes, and snippets.

@bgweber
Created January 21, 2019 01:56
Show Gist options
  • Save bgweber/de662707b93b2ae6e1978d2ebe7fdd23 to your computer and use it in GitHub Desktop.
Save bgweber/de662707b93b2ae6e1978d2ebe7fdd23 to your computer and use it in GitHub Desktop.
from sklearn.linear_model import LinearRegression
from scipy.stats.stats import pearsonr
# split into data and label arrays
y = boston_pd['target']
X = boston_pd.drop(['target'], axis=1)
# create training (~80%) and test data sets
X_train = X[:400]
X_test = X[400:]
y_train = y[:400]
y_test = y[400:]
# train a classifier
lr = LinearRegression()
model = lr.fit(X_train, y_train)
# make predictions
y_pred = model.predict(X_test)
# error metrics
r = pearsonr(y_pred, y_test)
mae = sum(abs(y_pred - y_test))/len(y_test
print("R-sqaured: " + str(r[0]**2))
print("MAE: " + str(mae))
@NickRoss
Copy link

NickRoss commented Jan 21, 2019

mae = sum(abs(y_pred - y_test))/len(y_test
=> needs trailing parenthesis.

Too lazy to do a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment