Evaluation Metrics
Mean Absolute Error
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).
Learning path
Prerequisite: Mean Squared Error.
By hand
y_true=[3,5,2,8,7], y_pred=[4,5,1,6,7]. Errors: |3-4|=1, |5-5|=0, |2-1|=1, |8-6|=2, |7-7|=0. MAE=(1+0+1+2+0)/5=0.8.
naive.py
Replay: real traced execution (multi-file project)
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))
y_true ← [3, 5, 2, 8, 7]
1y_true = [3, 5, 2, 8, 7]2y_pred = [4, 5, 1, 6, 7]values this step[3, 5, 2, 8, 7]y_truey_pred ← [4, 5, 1, 6, 7]
1y_true = [3, 5, 2, 8, 7]2y_pred = [4, 5, 1, 6, 7]3n = len(y_true)values this step[4, 5, 1, 6, 7]y_predn ← 5
2y_pred = [4, 5, 1, 6, 7]3n = len(y_true)4total = 0.0values this step5ntotal ← 0.0
3n = len(y_true)4total = 0.05for i in range(n):values this step0.0totali ← 0
4total = 0.05for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])values this step0itotal ← 1.0
5for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])7mae = total / nvalues this step0.0 → 1.0totali ← 1
4total = 0.05for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])values this step0 → 1itotal = total + abs(y_true[i] - y_pred[i])
5for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])7mae = total / ni ← 2
4total = 0.05for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])values this step1 → 2itotal ← 2.0
5for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])7mae = total / nvalues this step1.0 → 2.0totali ← 3
4total = 0.05for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])values this step2 → 3itotal ← 4.0
5for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])7mae = total / nvalues this step2.0 → 4.0totali ← 4
4total = 0.05for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])values this step3 → 4itotal = total + abs(y_true[i] - y_pred[i])
5for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])7mae = total / nfor i in range(n):
4total = 0.05for i in range(n):6 total = total + abs(y_true[i] - y_pred[i])mae ← 0.8
6 total = total + abs(y_true[i] - y_pred[i])7mae = total / n8print('RESULT:', round(mae, 4))values this step0.8maestdout ← RESULT: 0.8
7mae = total / n8print('RESULT:', round(mae, 4))values this stepRESULT: 0.8stdout
With scikit-learn
mean_absolute_error(y_true, y_pred) sums absolute differences and divides
by n in one call.
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). Mean Squared Error squares errors, so large errors dominate; MAE treats all errors proportionally.
abs()is a Python builtin; no import needed. Eachabs(y_true[i] - y_pred[i])call appears as a single trace step.- Mean Absolute Deviation measures spread around the sample mean; MAE measures error around model predictions.