Skip to content

Instantly share code, notes, and snippets.

@otaviomguerra
Created July 31, 2018 01:27
Show Gist options
  • Save otaviomguerra/c9dc592ff18dce1697612fbeb781631d to your computer and use it in GitHub Desktop.
Save otaviomguerra/c9dc592ff18dce1697612fbeb781631d to your computer and use it in GitHub Desktop.
Simple regression using RMSE and R² to evaluate
# 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