← back

Implement Hard Voting Classifier

#305 · Machine Learning · Easy

⊣ Solve on deep-ml.com

Problem

Implement a hard voting classifier. Given predictions from multiple classifiers, the final prediction for each sample is the class that receives the most votes (majority voting).

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
from collections import Counter

def hard_voting(predictions: list[list]) -> list:
    """
    predictions: list of arrays, one per classifier.
                 Each array has one prediction per sample.
    Returns: list of final predictions using majority vote.
    """
    n_samples = len(predictions[0])
    final_predictions = []

    for i in range(n_samples):
        votes = [clf_preds[i] for clf_preds in predictions]
        counter = Counter(votes)
        winner = counter.most_common(1)[0][0]
        final_predictions.append(winner)

    return final_predictions

Explanation

  1. For each sample, collect predictions from all classifiers.
  2. Count the votes for each class using a Counter.
  3. The class with the most votes wins. In case of a tie, most_common returns the first one encountered.
  4. Hard voting is simple and works well when individual classifiers are diverse and reasonably accurate. It does not use prediction probabilities.

Complexity

  • Time: O(n * k) where n is the number of samples and k is the number of classifiers
  • Space: O(k) for vote counting per sample