SKLearner Home | About | Contact | Examples

Configure SGDClassifier "loss" Parameter

The loss parameter in scikit-learn’s SGDClassifier determines the loss function used for model training.

Stochastic Gradient Descent (SGD) is an optimization algorithm that iteratively updates model parameters to minimize a loss function. It’s particularly useful for large-scale and online learning problems.

The loss parameter specifies the function used to compute the loss between the predicted and true values. This choice significantly impacts the model’s behavior and performance on different types of classification problems.

The default value for loss is ‘hinge’, which gives a linear SVM. Other common options include ’log_loss’ for logistic regression, ‘modified_huber’ for smoothed hinge loss, and ‘perceptron’ for the perceptron algorithm.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import accuracy_score
import numpy as np

# Generate synthetic dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=10,
                           n_redundant=5, 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 with different loss functions
loss_functions = ['hinge', 'log_loss', 'modified_huber', 'perceptron']
results = []

for loss in loss_functions:
    sgd = SGDClassifier(loss=loss, random_state=42)
    sgd.fit(X_train, y_train)
    y_pred = sgd.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    results.append((loss, accuracy))
    print(f"Loss: {loss}, Accuracy: {accuracy:.3f}")

# Find best model
best_model = max(results, key=lambda x: x[1])
print(f"\nBest model: {best_model[0]} (Accuracy: {best_model[1]:.3f})")

Running the example gives an output like:

Loss: hinge, Accuracy: 0.823
Loss: log_loss, Accuracy: 0.795
Loss: modified_huber, Accuracy: 0.768
Loss: perceptron, Accuracy: 0.770

Best model: hinge (Accuracy: 0.823)

The key steps in this example are:

  1. Generate a synthetic binary classification dataset
  2. Split the data into train and test sets
  3. Train SGDClassifier models with different loss functions
  4. Evaluate each model’s accuracy and log loss on the test set
  5. Identify the best-performing model based on accuracy

Some tips and heuristics for setting the loss parameter:

Issues to consider:



See Also