🎓 A1Learner
🔍
0 of 50 lessons completed

Python ML How Good Is a Regression Model?

A regression metric turns the prediction errors into one number. Four metrics look at the same errors in four ways.

One error at a time

Each error is the real value minus the prediction. Squaring removes the sign.

import numpy as np

actual = np.array([3.0, 5.0, 2.0, 7.0, 4.0])
predicted = np.array([2.5, 5.0, 3.0, 6.0, 4.5])
errors = actual - predicted
print(errors)
print(np.round(errors ** 2, 2))

MSE and RMSE on five numbers

The squared errors average to 0.5. The square root brings the error back to plain units.

import numpy as np

actual = np.array([3.0, 5.0, 2.0, 7.0, 4.0])
predicted = np.array([2.5, 5.0, 3.0, 6.0, 4.5])
errors = actual - predicted
print(round(float(np.mean(errors ** 2)), 4))
print(round(float(np.sqrt(np.mean(errors ** 2))), 4))

A perfect model scores R2 of 1

When every prediction is exact, the errors are zero and R2 is 1.

import numpy as np
from sklearn.metrics import r2_score

actual = np.array([3.0, 5.0, 2.0, 7.0, 4.0])
print(round(r2_score(actual, actual), 4))

Example

import numpy as np
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.model_selection import train_test_split

actual = np.array([3.0, 5.0, 2.0, 7.0, 4.0])
predicted = np.array([2.5, 5.0, 3.0, 6.0, 4.5])
errors = actual - predicted
mse_hand = np.mean(errors ** 2)
rmse_hand = np.sqrt(mse_hand)
mae_hand = np.mean(np.abs(errors))
r2_hand = 1 - np.sum(errors ** 2) / np.sum((actual - actual.mean()) ** 2)
print("Hand MSE:", round(float(mse_hand), 4), "sklearn MSE:", round(mean_squared_error(actual, predicted), 4))
print("Hand RMSE:", round(float(rmse_hand), 4), "sklearn RMSE:", round(float(np.sqrt(mean_squared_error(actual, predicted))), 4))
print("Hand MAE:", round(float(mae_hand), 4), "sklearn MAE:", round(mean_absolute_error(actual, predicted), 4))
print("Hand R2:", round(float(r2_hand), 4), "sklearn R2:", round(r2_score(actual, predicted), 4))
X, y = load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
pred = model.predict(X_test)
print("Diabetes MSE:", round(mean_squared_error(y_test, pred), 2))
print("Diabetes RMSE:", round(float(np.sqrt(mean_squared_error(y_test, pred))), 2))
print("Diabetes MAE:", round(mean_absolute_error(y_test, pred), 2))
print("Diabetes R2:", round(r2_score(y_test, pred), 4))

Key points

  • MSE: the mean of the squared errors, short for mean squared error
  • RMSE: the square root of the MSE, back in the units of the target
  • MAE: the mean of the absolute errors, the average miss in plain units
  • R2: the share of the target pattern the model explains, from 0 to 1
  • Use MSE to punish big misses more than small ones
  • Use RMSE when you want the error in the original units
  • Use MAE when every miss should count the same

In this example, five small numbers are scored by hand first. The hand MSE of 0.5 matches the sklearn MSE of 0.5, and the same holds for RMSE, MAE, and R2. Then a linear model is trained on the Diabetes data, which holds 442 patients and a disease score for each one. Its test MSE is 2848.31 and its test R2 is 0.4849, so the model explains about half of the disease score pattern. The four metrics agree with each other because they all read the same errors.

Note

A lower MSE, RMSE, or MAE is better, while a higher R2 is better.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which metric gives the average error in the original units?