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
mean_absolute_erroris in the target's own units.root_mean_squared_errorpunishes large misses harder.r2_scoreis 1.0 for a perfect fit and 0.0 for predicting the mean.
Keywords and builtins used here
print
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.
Step 4 of 4 in Scoring, step 19 of 25 in Machine learning with scikit-learn.