Before you start
Combine the class predictions of several models by majority vote.
An ensemble of classifiers often beats each of its members, because their errors do not all happen on the same examples. The simplest way to combine them is hard voting: each model casts one vote per example, and the label with the most votes wins.
You get a (n_models, n_samples) array of integer labels. Return, for each
sample, the label that appears most often in its column. When two or more
labels tie, return the smallest of them.
def majority_vote(predictions):
Input
predictions = np.array([
[0, 1, 2, 1],
[0, 2, 2, 0],
[1, 1, 0, 0],
])
majority_vote(predictions)
Output
array([0, 1, 2, 0])
Reasoning
Read each column from top to bottom. Column 0 has votes 0, 0, 1, so 0
wins. Column 1 has 1, 2, 1, so 1 wins. Column 2 has 2, 2, 0, so 2
wins. Column 3 has 1, 0, 0, so 0 wins.
(n_samples,).Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell