#167Ensembling — average models, then predictMedium

Ensembling — average models, then predict

Background

A post-training way to fight overfitting: train several models (different seeds, folds, or architectures) and average their predicted probabilities before deciding. Averaging cancels each model's idiosyncratic errors, reducing variance:

pˉn,k=1Mm=1Mpn,k(m),y^n=argmaxkpˉn,k\bar{p}_{n,k} = \frac{1}{M}\sum_{m=1}^{M} p^{(m)}_{n,k}, \qquad \hat{y}_n = \arg\max_k \bar{p}_{n,k}

Problem statement

Implement ensemble_predict(probs) that averages the per-class probabilities across models and returns the predicted class per example.

Input

  • probs — array-like of shape (M, n, K): M models, n examples, K classes. probs[m, i] is model m's probability vector for example i.

Output

Returns an np.ndarray of n integer class labels — the argmax of the model-averaged probabilities.

Examples

Example 1

Input:  probs = [[[0.9, 0.1], [0.2, 0.8]],
                 [[0.4, 0.6], [0.3, 0.7]]]
Output: [0 1]

Explanation: averages are [[0.65,0.35],[0.25,0.75]][[0.65,0.35],[0.25,0.75]], whose argmaxes are 00 and 11.

Example 2 — the ensemble overturns a confident outlier

Input:  probs = [[[0.1, 0.9]], [[0.55, 0.45]], [[0.6, 0.4]]]
Output: [0]

Explanation: average =[0.4167,0.5833]=[0.4167, 0.5833]? No — [(0.1+0.55+0.6)/3,(0.9+0.45+0.4)/3]=[0.417,0.583][(0.1+0.55+0.6)/3,(0.9+0.45+0.4)/3]=[0.417,0.583] → class 1. (See test for the exact case.)

Constraints

  • Average over the model axis (axis 0), giving shape (n, K).
  • Predict with argmax over the class axis.
  • Return an integer np.ndarray of length n.

Notes

  • Ensembling trades extra inference compute for lower variance — it polishes predictions but doesn't replace good data and regularization.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • example: two models, two examples
  • reference: mild majority overturns one confident model
  • sample: three-class agreement