PIXELBANKv9.1.0
Menu

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:
  • The input predictions is a 2D list where each row represents a model's predictions and each column represents a data point.
  • For each data point (column), we count the occurrences of each class label:
    • For the 1st data point, the counts are 2 (for class 1) and 1 (for class 0).
    • For the 2nd data point, the counts are 1 (for class 1) and 2 (for class 0).
    • For the 3rd data point, the counts are 3 (for class 1) and 0 (for class 0).
  • We apply the majority vote for each data point:
    • The 1st data point has a tie, but since 0 is the smallest class label in case of a tie, it's not selected; instead, the smallest label among the tied ones is chosen which is 11.
    • The 2nd data point has a majority vote for class 00.
    • The 3rd data point has a majority vote for class 11.
  • The final output is the list of majority votes for each data point: [1,0,1][1, 0, 1]

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
solution.py

Test Results

0/0
Run code to see test results.