Regression Metrics: MAE, RMSE, R²

easy
Unit 6 · Hypothesis Testing I0/3 to milestone

How good is a forecast? Three numbers answer it from different angles:

MAE=1n∑∣yi−y^i∣,RMSE=1n∑(yi−y^i)2,R2=1−∑(yi−y^i)2∑(yi−yˉ)2\text{MAE} = \frac{1}{n}\sum |y_i - \hat y_i|, \qquad \text{RMSE} = \sqrt{\frac{1}{n}\sum (y_i - \hat y_i)^2}, \qquad R^2 = 1 - \frac{\sum (y_i - \hat y_i)^2}{\sum (y_i - \bar y)^2}

Implement regression_metrics(y_true, y_pred) returning a dict with keys "mae", "rmse" and "r2".

If every y_true value is the same, the R2R^2 denominator is zero: return 1.0 if the predictions are also perfect, otherwise 0.0. Raise ValueError if the inputs have different lengths or are empty.

#evaluation#regression#numpy2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Regression Metrics: MAE, RMSE, R² · TechGradify