Model Workflow
Compare Two Model Scores
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)))
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_ascores_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.0values this step[0.7, 0.9, 0.82]scores_btotal_a ← 0.0
2scores_b = [0.7, 0.9, 0.82]3total_a = 0.04for s in scores_a:values this step0.0total_as ← 0.8
3total_a = 0.04for s in scores_a:5 total_a = total_a + svalues this step0.8stotal_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_as ← 0.75
3total_a = 0.04for s in scores_a:5 total_a = total_a + svalues this step0.8 → 0.75stotal_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_as ← 0.85
3total_a = 0.04for s in scores_a:5 total_a = total_a + svalues this step0.75 → 0.85stotal_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_afor s in scores_a:
3total_a = 0.04for s in scores_a:5 total_a = total_a + smean_a ← 0.7999999999999999
5 total_a = total_a + s6mean_a = total_a / len(scores_a)7total_b = 0.0values this step0.7999999999999999mean_atotal_b ← 0.0
6mean_a = total_a / len(scores_a)7total_b = 0.08for s in scores_b:values this step0.0total_bs ← 0.7
7total_b = 0.08for s in scores_b:9 total_b = total_b + svalues this step0.85 → 0.7stotal_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_bs ← 0.9
7total_b = 0.08for s in scores_b:9 total_b = total_b + svalues this step0.7 → 0.9stotal_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_bs ← 0.82
7total_b = 0.08for s in scores_b:9 total_b = total_b + svalues this step0.9 → 0.82stotal_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_bfor s in scores_b:
7total_b = 0.08for s in scores_b:9 total_b = total_b + smean_b ← 0.8066666666666666
9 total_b = total_b + s10mean_b = total_b / len(scores_b)11if mean_a >= mean_b:values this step0.8066666666666666mean_bif mean_a >= mean_b:
10mean_b = total_b / len(scores_b)11if mean_a >= mean_b:12 winner = 'A'winner ← 'B'
14else:15 winner = 'B'16 best = mean_bvalues this step'B'winnerbest ← 0.8066666666666666
15 winner = 'B'16 best = mean_b17print('RESULT:', (winner, round(best, 4)))values this step0.8066666666666666beststdout ← 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_bfavours 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.