SKLearner Home | About | Contact | Examples

Scikit-Learn hinge_loss() Metric

Hinge loss is a metric used to evaluate the performance of classifiers, particularly support vector machines (SVMs).

It measures the distance between the actual and predicted labels, penalizing incorrect classifications and those that are not confidently correct.

Hinge loss is calculated as the maximum of 0 and 1 minus the product of the actual and predicted label. It is commonly used for binary classification problems but is not suitable for probability-based classifiers as it expects raw class scores.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import hinge_loss

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train an SVM classifier
clf = SVC(kernel='linear', C=1, random_state=42)
clf.fit(X_train, y_train)

# Predict decision function values on test set
decision_function = clf.decision_function(X_test)

# Calculate hinge loss
loss = hinge_loss(y_test, decision_function)
print(f"Hinge Loss: {loss:.2f}")

Running the example gives an output like:

Hinge Loss: 0.38

The steps are as follows:

  1. Generate a synthetic binary classification dataset using make_classification().
  2. Split the dataset into training and test sets using train_test_split().
  3. Train an SVC classifier on the training set with a linear kernel.
  4. Predict decision function values on the test set using decision_function().
  5. Calculate the hinge loss using hinge_loss() by comparing the true labels to the decision function values.
  6. Print the hinge loss to evaluate the classifier’s performance.

First, we generate a synthetic binary classification dataset using the make_classification() function from scikit-learn. This function creates a dataset with 1000 samples and 2 classes, allowing us to simulate a classification problem without using real-world data.

Next, we split the dataset into training and test sets using the train_test_split() function. This step is crucial for evaluating the performance of our classifier on unseen data. We use 80% of the data for training and reserve 20% for testing.

With our data prepared, we train an SVM classifier using the SVC class from scikit-learn. We specify a linear kernel and set the regularization parameter C to 1. The fit() method is called on the classifier object, passing in the training features (X_train) and labels (y_train) to learn the underlying patterns in the data.

After training, we use the trained classifier to predict decision function values on the test set by calling the decision_function() method with X_test. This generates raw scores for each sample in the test set.

Finally, we evaluate the performance of our classifier using the hinge_loss() function. This function takes the true labels (y_test) and the decision function values (decision_function) as input and calculates the hinge loss. The resulting hinge loss score is printed, giving us a quantitative measure of our classifier’s performance.

This example demonstrates how to use the hinge_loss() function from scikit-learn to evaluate the performance of a binary classification model.



See Also