Given per-fold CV scores for two models, compute each mean and select the model with the higher mean. Library: np.argmax over the two means. RESULT: (winning model name, mean score).

By hand

scores_a=[0.8,0.75,0.85] → mean=0.8; scores_b=[0.7,0.9,0.82] → mean≈0.8067. B wins (0.8067 > 0.8).

naive.py
Replay: real traced execution (multi-file project)
scores_a = [0.8, 0.75, 0.85]
scores_b = [0.7, 0.9, 0.82]
total_a = 0.0
for s in scores_a:
    total_a = total_a + s
mean_a = total_a / len(scores_a)
total_b = 0.0
for s in scores_b:
    total_b = total_b + s
mean_b = total_b / len(scores_b)
if mean_a >= mean_b:
    winner = 'A'
    best = mean_a
else:
    winner = 'B'
    best = mean_b
print('RESULT:', (winner, round(best, 4)))
  1. scores_a ← [0.8, 0.75, 0.85]

    1scores_a = [0.8, 0.75, 0.85]2scores_b = [0.7, 0.9, 0.82]
    values this step[0.8, 0.75, 0.85]scores_a
  2. scores_b ← [0.7, 0.9, 0.82]

    1scores_a = [0.8, 0.75, 0.85]2scores_b = [0.7, 0.9, 0.82]3total_a = 0.0
    values this step[0.7, 0.9, 0.82]scores_b
  3. total_a ← 0.0

    2scores_b = [0.7, 0.9, 0.82]3total_a = 0.04for s in scores_a:
    values this step0.0total_a
  4. s ← 0.8

    3total_a = 0.04for s in scores_a:5    total_a = total_a + s
    values this step0.8s
  5. total_a ← 0.8

    4for s in scores_a:5    total_a = total_a + s6mean_a = total_a / len(scores_a)
    values this step0.0 0.8total_a
  6. s ← 0.75

    3total_a = 0.04for s in scores_a:5    total_a = total_a + s
    values this step0.8 0.75s
  7. total_a ← 1.55

    4for s in scores_a:5    total_a = total_a + s6mean_a = total_a / len(scores_a)
    values this step0.8 1.55total_a
  8. s ← 0.85

    3total_a = 0.04for s in scores_a:5    total_a = total_a + s
    values this step0.75 0.85s
  9. total_a ← 2.4

    4for s in scores_a:5    total_a = total_a + s6mean_a = total_a / len(scores_a)
    values this step1.55 2.4total_a
  10. for s in scores_a:

    3total_a = 0.04for s in scores_a:5    total_a = total_a + s
  11. mean_a ← 0.7999999999999999

    5    total_a = total_a + s6mean_a = total_a / len(scores_a)7total_b = 0.0
    values this step0.7999999999999999mean_a
  12. total_b ← 0.0

    6mean_a = total_a / len(scores_a)7total_b = 0.08for s in scores_b:
    values this step0.0total_b
  13. s ← 0.7

    7total_b = 0.08for s in scores_b:9    total_b = total_b + s
    values this step0.85 0.7s
  14. total_b ← 0.7

    8for s in scores_b:9    total_b = total_b + s10mean_b = total_b / len(scores_b)
    values this step0.0 0.7total_b
  15. s ← 0.9

    7total_b = 0.08for s in scores_b:9    total_b = total_b + s
    values this step0.7 0.9s
  16. total_b ← 1.6

    8for s in scores_b:9    total_b = total_b + s10mean_b = total_b / len(scores_b)
    values this step0.7 1.6total_b
  17. s ← 0.82

    7total_b = 0.08for s in scores_b:9    total_b = total_b + s
    values this step0.9 0.82s
  18. total_b ← 2.42

    8for s in scores_b:9    total_b = total_b + s10mean_b = total_b / len(scores_b)
    values this step1.6 2.42total_b
  19. for s in scores_b:

    7total_b = 0.08for s in scores_b:9    total_b = total_b + s
  20. mean_b ← 0.8066666666666666

    9    total_b = total_b + s10mean_b = total_b / len(scores_b)11if mean_a >= mean_b:
    values this step0.8066666666666666mean_b
  21. if mean_a >= mean_b:

    10mean_b = total_b / len(scores_b)11if mean_a >= mean_b:12    winner = 'A'
  22. winner ← 'B'

    14else:15    winner = 'B'16    best = mean_b
    values this step'B'winner
  23. best ← 0.8066666666666666

    15    winner = 'B'16    best = mean_b17print('RESULT:', (winner, round(best, 4)))
    values this step0.8066666666666666best
  24. stdout ← RESULT: ('B', 0.8067)

    16    best = mean_b17print('RESULT:', (winner, round(best, 4)))
    values this stepRESULT: ('B', 0.8067)stdout

With NumPy

np.mean computes each mean; np.argmax returns the index of the larger value, selecting the winning model without an explicit if/else.

library.py
import numpy as np
from dalib.display import set_display
set_display()

names = ['A', 'B']
scores_a = [0.8, 0.75, 0.85]
scores_b = [0.7, 0.9, 0.82]
means = [float(np.mean(scores_a)), float(np.mean(scores_b))]
best_i = int(np.argmax(means))
print('means:', [round(m, 4) for m in means])
print('RESULT:', (names[best_i], round(means[best_i], 4)))
means: [0.8, 0.8067]
RESULT: ('B', 0.8067)

Implementation notes

  • if mean_a >= mean_b favours A on exact ties; in practice, prefer the simpler model when scores are equal.
  • CV mean score is a selection heuristic — a held-out test set is needed to confirm the winner generalises (selecting on validation can overfit to the validation folds).
  • Cross-reference: accuracy-score (ch07) for the per-fold metric; cross-val-fold-manual (this chapter) for how the fold scores are obtained.