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))
  1. 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_true
  2. y_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_pred
  3. n ← 5

    2y_pred = [4, 5, 1, 6, 7]3n = len(y_true)4total = 0.0
    values this step5n
  4. total ← 0.0

    3n = len(y_true)4total = 0.05for i in range(n):
    values this step0.0total
  5. i ← 0

    4total = 0.05for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])
    values this step0i
  6. total ← 1.0

    5for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])7mae = total / n
    values this step0.0 1.0total
  7. i ← 1

    4total = 0.05for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])
    values this step0 1i
  8. total = 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 / n
  9. i ← 2

    4total = 0.05for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])
    values this step1 2i
  10. total ← 2.0

    5for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])7mae = total / n
    values this step1.0 2.0total
  11. i ← 3

    4total = 0.05for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])
    values this step2 3i
  12. total ← 4.0

    5for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])7mae = total / n
    values this step2.0 4.0total
  13. i ← 4

    4total = 0.05for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])
    values this step3 4i
  14. total = 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 / n
  15. for i in range(n):

    4total = 0.05for i in range(n):6    total = total + abs(y_true[i] - y_pred[i])
  16. mae ← 0.8

    6    total = total + abs(y_true[i] - y_pred[i])7mae = total / n8print('RESULT:', round(mae, 4))
    values this step0.8mae
  17. stdout ← 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. Each abs(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.