Seed-Averaging Ensemble Size Curve

Quick Overview

Implement a seed-averaging ensemble in Python: train one classifier per random seed, stack the validation probabilities, and score the mean of the first k members with accuracy or log loss. It tests NumPy array handling, incremental averaging, and why ensemble gains flatten as members are added.

Seed-Averaging Ensemble Size Curve

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement three functions that measure how a seed-averaging ensemble changes as members are added. - `seed_member_probabilities(make_model, X_train, y_train, X_val, seeds)` fits `make_model(seed)` on the training data for each seed (in order) and stacks `predict_proba(X_val)` into shape `(n_seeds, n_val, n_classes)`. - `cumulative_ensemble_scores(member_probs, y_val, metric="log_loss")` returns a length-`M` float array whose entry `k-1` scores the arithmetic mean of the first `k` members. - `seed_averaging_curve(make_model, X_train, y_train, X_val, y_val, seeds, metric="log_loss")` chains the two. The metrics are defined as follows: - `metric="accuracy"`: mean of `argmax` predictions equal to `y_val` (ties go to the lowest class index). - `metric="log_loss"` (default): `-mean(log(p_true))` with `p_true` clipped to `[1e-15, 1.0]`. - Any other metric raises `ValueError`. Example: ```python probs = [[[0.6, 0.4], [0.4, 0.6]], [[0.3, 0.7], [0.8, 0.2]], [[0.9, 0.1], [0.9, 0.1]]] cumulative_ensemble_scores(probs, [0, 0], metric="accuracy") # -> array([0.5, 0.5, 1.0]) cumulative_ensemble_scores([[[0.2, 0.8]], [[0.6, 0.4]]], [1]) # -> array([-log(0.8), -log(0.6)]) which is about array([0.2231, 0.5108]) ``` ```hint Avoid redoing work Re-averaging all k members at every step costs quadratic time in the number of members. Think about what you can carry over from step k-1 to step k. ``` ### Constraints & Assumptions - `make_model(seed)` returns an unfitted scikit-learn-style classifier with `fit` and `predict_proba`. - `member_probs` has shape `(M, n_val, n_classes)` and every member uses the same class column order. - `y_val` holds integer class indices into the probability columns. ### What a Strong Answer Covers - Fits a fresh model for each seed in the given order and stacks the probabilities along a new first axis. - Builds each prefix mean incrementally so the full curve takes one pass over the members. - Implements both metrics exactly as specified, including the lowest-index tie rule and the clip to `[1e-15, 1.0]`. - Rejects an unknown metric with `ValueError`. - Handles or names edge cases such as an empty seed list, label encoding that does not match the probability columns, and a curve that is not monotone. ### Follow-up Questions - Why does the curve flatten as members are added? - Is the ensemble log loss guaranteed to be no worse than the average member's? What about accuracy? - How would you increase diversity beyond changing seeds?

Overview: Implement a seed-averaging ensemble in Python: train one classifier per random seed, stack the validation probabilities, and score the mean of the first k members with accuracy or log loss. It tests NumPy array handling, incremental averaging, and why ensemble gains flatten as members are added.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement three functions that measure how a seed-averaging ensemble changes as members are added.

  • seed_member_probabilities(make_model, X_train, y_train, X_val, seeds) fits make_model(seed) on the training data for each seed (in order) and stacks predict_proba(X_val) into shape (n_seeds, n_val, n_classes) .
  • cumulative_ensemble_scores(member_probs, y_val, metric="log_loss") returns a length- M float array whose entry k-1 scores the arithmetic mean of the first k members.
  • seed_averaging_curve(make_model, X_train, y_train, X_val, y_val, seeds, metric="log_loss") chains the two.

The metrics are defined as follows:

  • metric="accuracy" : mean of argmax predictions equal to y_val (ties go to the lowest class index).
  • metric="log_loss" (default): -mean(log(p_true)) with p_true clipped to [1e-15, 1.0] .
  • Any other metric raises ValueError .

Example:

probs = [[[0.6, 0.4], [0.4, 0.6]],
         [[0.3, 0.7], [0.8, 0.2]],
         [[0.9, 0.1], [0.9, 0.1]]]
cumulative_ensemble_scores(probs, [0, 0], metric="accuracy")
# -> array([0.5, 0.5, 1.0])

cumulative_ensemble_scores([[[0.2, 0.8]], [[0.6, 0.4]]], [1])
# -> array([-log(0.8), -log(0.6)])  which is about array([0.2231, 0.5108])

Constraints & Assumptions

  • make_model(seed) returns an unfitted scikit-learn-style classifier with fit and predict_proba .
  • member_probs has shape (M, n_val, n_classes) and every member uses the same class column order.
  • y_val holds integer class indices into the probability columns.

What a Strong Answer Covers Guidance

  • Fits a fresh model for each seed in the given order and stacks the probabilities along a new first axis.
  • Builds each prefix mean incrementally so the full curve takes one pass over the members.
  • Implements both metrics exactly as specified, including the lowest-index tie rule and the clip to [1e-15, 1.0] .
  • Rejects an unknown metric with ValueError .
  • Handles or names edge cases such as an empty seed list, label encoding that does not match the probability columns, and a curve that is not monotone.

Follow-up Questions Guidance

  • Why does the curve flatten as members are added?
  • Is the ensemble log loss guaranteed to be no worse than the average member's? What about accuracy?
  • How would you increase diversity beyond changing seeds?