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).
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_predictionsmost_common returns the first one encountered.