Skip to content

Instantly share code, notes, and snippets.

@64lines
Last active November 6, 2017 19:56
Show Gist options
  • Select an option

  • Save 64lines/05e8b5fe1f09f4336ed61d1295bab98f to your computer and use it in GitHub Desktop.

Select an option

Save 64lines/05e8b5fe1f09f4336ed61d1295bab98f to your computer and use it in GitHub Desktop.
[PYTHON][SKLEARN] Measuring Accuracy Linear Regression Models
# Import necessary modules
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
# Create training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=42)
# Create the regressor: reg_all
reg_all = LinearRegression()
# Fit the regressor to the training data
reg_all.fit(X_train, y_train)
# Predict on the test data: y_pred
y_pred = reg_all.predict(X_test)
# Compute and print R^2 and RMSE
print("R^2: {}".format(reg_all.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
print("Root Mean Squared Error: {}".format(rmse))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment