v9.1.1
Menu

Bagging Aggregation

mediumFREEEnsemble MethodsMachine Learning 1

Implement bagging aggregation for an ensemble of model predictions.

Given predictions from mm models for nn data points:

  • For classification (mode="classify"): return the majority vote for each data point
  • For regression (mode="regress"): return the mean prediction for each data point

In case of a tie in majority voting, return the smallest class label.

Return a list of aggregated predictions, rounded to 4 decimal places for regression.

Example

Input
predictions = [[1, 0, 1], [0, 0, 1], [1, 1, 1]]
mode = "classify"
Output
[1, 0, 1]
Reasoning
  • predictions holds one row per model and one column per data point. Here m=3m = 3 models each label n=3n = 3 points, so the answer has 3 entries, one per column. mode is "classify", so each column is decided by majority vote.
  • Point 1 (first column): the three models say 1,0,11, 0, 1. Label 11 gets 2 votes, label 00 gets 1, so the vote is 11.
  • Point 2: the models say 0,0,10, 0, 1. Label 00 wins 22 to 11, so the vote is 00.
  • Point 3: all three say 11, a unanimous 33 to 00, so the vote is 11.
  • Reading the winners across the columns gives [1, 0, 1]. Notice that point 1 is not a tie: a tie needs two labels with the same top count, e.g. 0,10, 1 from two models, and then the smaller label 00 wins.
  • In regression mode the column is averaged instead: models predicting 2.0,4.0,3.02.0, 4.0, 3.0 give 93=3.0\frac{9}{3} = 3.0, rounded to 4 decimal places.

Constraints

  • predictions: 2D list (m models x n data points)
  • mode: "classify" or "regress"
  • Return list of n aggregated predictions
  • For regression, round to 4 decimal places
  • For ties in classification, use the smallest label

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.