SKLearner Home | About | Contact | Examples

Configure SGDRegressor "loss" Parameter

The loss parameter in scikit-learn’s SGDRegressor determines the loss function to be used for optimization during training.

Stochastic Gradient Descent (SGD) is a simple yet efficient approach to fit linear models. It is particularly useful when dealing with large datasets as it processes samples sequentially.

The loss parameter defines the criterion that the model tries to optimize. Different loss functions can lead to different behaviors and performances of the model, depending on the characteristics of the data and the problem at hand.

The default value for loss is ‘squared_error’. Common options include ‘huber’, ’epsilon_insensitive’, and ‘squared_epsilon_insensitive’.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDRegressor
from sklearn.metrics import mean_squared_error
import numpy as np

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, 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 = ['squared_error', 'huber', 'epsilon_insensitive', 'squared_epsilon_insensitive']
mse_scores = []

for loss in loss_functions:
    sgd = SGDRegressor(loss=loss, random_state=42)
    sgd.fit(X_train, y_train)
    y_pred = sgd.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    mse_scores.append(mse)
    print(f"Loss function: {loss}, MSE: {mse:.4f}")

# Find the best performing loss function
best_loss = loss_functions[np.argmin(mse_scores)]
print(f"\nBest performing loss function: {best_loss}")

Running the example gives an output like:

Loss function: squared_error, MSE: 0.0096
Loss function: huber, MSE: 10918.3162
Loss function: epsilon_insensitive, MSE: 0.0094
Loss function: squared_epsilon_insensitive, MSE: 0.0102

Best performing loss function: epsilon_insensitive

The key steps in this example are:

  1. Generate a synthetic regression dataset
  2. Split the data into train and test sets
  3. Train SGDRegressor models with different loss functions
  4. Evaluate the mean squared error of each model on the test set
  5. Identify the best performing loss function

Some tips for choosing loss functions:

Issues to consider:



See Also