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

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.

naive.py
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)))
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.