Implement a soft voting classifier. Instead of counting class votes, average the predicted class probabilities from each classifier and pick the class with the highest average probability.
import numpy as np
def soft_voting(probabilities: list[np.ndarray],
weights: list[float] = None) -> np.ndarray:
"""
probabilities: list of arrays of shape (n_samples, n_classes),
one per classifier.
weights: optional list of weights for each classifier.
Returns: array of final class predictions.
"""
n_classifiers = len(probabilities)
if weights is None:
weights = [1.0 / n_classifiers] * n_classifiers
else:
total = sum(weights)
weights = [w / total for w in weights]
weighted_sum = np.zeros_like(probabilities[0], dtype=float)
for i, probs in enumerate(probabilities):
weighted_sum += weights[i] * np.array(probs)
return np.argmax(weighted_sum, axis=1)