Compute MAE: mean of absolute differences between true and predicted values. Loop accumulates abs(y_true[i] - y_pred[i]); divide by n. Library: sklearn.metrics.mean_absolute_error(y_true, y_pred). RESULT: MAE (rounded).

By hand

With scikit-learn

mean_absolute_error(y_true, y_pred) sums absolute differences and divides by n in one call.

naive.py
y_true = [3, 5, 2, 8, 7]
y_pred = [4, 5, 1, 6, 7]
n = len(y_true)
total = 0.0
for i in range(n):
    total = total + abs(y_true[i] - y_pred[i])
mae = total / n
print('RESULT:', round(mae, 4))
library.py
from sklearn.metrics import mean_absolute_error
from dalib.display import set_display
set_display()

y_true = [3, 5, 2, 8, 7]
y_pred = [4, 5, 1, 6, 7]
errors = [abs(yt - yp) for yt, yp in zip(y_true, y_pred)]
print('errors:', errors)
mae = mean_absolute_error(y_true, y_pred)
print('RESULT:', round(float(mae), 4))
errors: [1, 0, 1, 2, 0]
RESULT: 0.8

Implementation notes

  • MAE = average absolute error; units match the target (here, same units as y_true). Cross-reference: mean-squared-error (ch03) — MSE squares errors, so large errors dominate; MAE treats all errors proportionally.
  • abs() is a Python builtin; no import needed. Each abs(y_true[i] - y_pred[i]) call appears as a single trace step.
  • Cross-reference: mean-abs-deviation (stats book) — MAD measures spread around the sample mean; MAE measures error around model predictions.