typestar

Regression metrics in Python

Error in the units you care about, and the share of variance explained.

from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_absolute_percentage_error
from sklearn.metrics import r2_score, root_mean_squared_error

y_true = [10.0, 12.0, 14.5, 19.0, 22.0]
y_pred = [10.5, 11.0, 15.0, 18.0, 24.0]

print(f"mae  {mean_absolute_error(y_true, y_pred):.3f}")
print(f"rmse {root_mean_squared_error(y_true, y_pred):.3f}")
print(f"mape {mean_absolute_percentage_error(y_true, y_pred):.3%}")
print(f"r2   {r2_score(y_true, y_pred):.3f}")

How it works

  1. mean_absolute_error is in the target's own units.
  2. root_mean_squared_error punishes large misses harder.
  3. r2_score is 1.0 for a perfect fit and 0.0 for predicting the mean.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
482
Tokens
114
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 62 seconds.

Type this snippet

Step 4 of 4 in Scoring, step 19 of 25 in Machine learning with scikit-learn.

← Previous Next →