← back

Implement Hinge Loss for SVM

#283 · Machine Learning · Easy

⊣ Solve on deep-ml.com

Problem

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

def hinge_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)
    return float(np.mean(losses))

Explanation

  1. For each sample, compute the product y_i * y_hat_i. If the prediction is correct and confident (product >= 1), the loss is 0.
  2. Otherwise, the loss is 1 - y_i * y_hat_i, penalizing incorrect or insufficiently confident predictions.
  3. Take the mean across all samples to get the average hinge loss.

Complexity

  • Time: O(n) where n is the number of samples
  • Space: O(n) for the intermediate losses array