Majority Vote

Easy
~15 min
code completion

Majority Vote (Hard Voting)

In an ensemble classifier, each model casts a vote. The final prediction is the majority vote — the class that appears most often across the ensemble.

For a matrix of predictions where each row is a sample and each column is one model's predictions:

predictions = np.array([[0, 1, 1],   # sample 0: models vote [0,1,1] → 1 wins
                        [1, 0, 1],   # sample 1: models vote [1,0,1] → 1 wins
                        [0, 0, 1]])  # sample 2: models vote [0,0,1] → 0 wins

Use np.apply_along_axis with np.bincount and np.argmax, or work row-by-row.

Your task:

Implement majority_vote(predictions) that returns a 1D array of final class predictions.

Example Tests

3 models, clear majority

Input: {"predictions":[[0,1,1],[1,0,1],[0,0,1]]}

Expected: [1,1,0]

All models agree

Input: {"predictions":[[1,1,1],[0,0,0]]}

Expected: [1,0]

5 models, 3-2 split

Input: {"predictions":[[0,0,0,1,1],[1,1,1,0,0]]}

Expected: [0,1]

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.