Implement the hinge loss function used in SVMs. Given a list of true labels y (values +1 or -1) and predicted scores y_hat, compute the average hinge loss: L = (1/n) sum(max(0, 1 - y_i y_hat_i)).
Solution
1
2
3
4
5
6
7
import numpy as np
defhinge_loss(y: list, y_hat: list) -> float:
y = np.array(y)
y_hat = np.array(y_hat)
losses = np.maximum(0, 1 - y * y_hat)
returnfloat(np.mean(losses))
Explanation
For each sample, compute the product y_i * y_hat_i. If the prediction is correct and confident (product >= 1), the loss is 0.
Otherwise, the loss is 1 - y_i * y_hat_i, penalizing incorrect or insufficiently confident predictions.
Take the mean across all samples to get the average hinge loss.