Implementation:Scikit learn Scikit learn SGDClassifier
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Stochastic Gradient Descent |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for training linear classifiers (SVM, logistic regression, etc.) using Stochastic Gradient Descent provided by scikit-learn.
Description
SGDClassifier implements regularized linear models with stochastic gradient descent learning, where the gradient of the loss is estimated one sample at a time and the model is updated with a decreasing learning rate schedule. It supports multiple loss functions including 'hinge' (linear SVM), 'log_loss' (logistic regression), 'modified_huber', 'squared_hinge', and 'perceptron'. The regularizer can be L2, L1, or Elastic Net. SGD is especially suitable for large-scale learning (>10,000 training samples) and supports incremental learning via partial_fit. The module also includes BaseSGD, SGDRegressor, and SGDOneClassSVM.
Usage
Use SGDClassifier when you have very large datasets that do not fit in memory and you need an efficient online/mini-batch classifier. It is also useful when you want to flexibly combine different loss functions and regularizers, or when you need incremental/online learning capabilities. By default, it fits a linear SVM, but it can emulate logistic regression, perceptron, and other linear classifiers by changing the loss function.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/linear_model/_stochastic_gradient.py
Signature
class SGDClassifier(BaseSGDClassifier):
def __init__(
self,
loss="hinge",
*,
penalty="l2",
alpha=0.0001,
l1_ratio=0.15,
fit_intercept=True,
max_iter=1000,
tol=1e-3,
shuffle=True,
verbose=0,
epsilon=DEFAULT_EPSILON,
n_jobs=None,
random_state=None,
learning_rate="optimal",
eta0=0.0,
power_t=0.5,
early_stopping=False,
validation_fraction=0.1,
n_iter_no_change=5,
class_weight=None,
warm_start=False,
average=False,
):
Import
from sklearn.linear_model import SGDClassifier
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| loss | str | No | Loss function: 'hinge', 'log_loss', 'modified_huber', 'squared_hinge', 'perceptron', etc. (default='hinge') |
| penalty | str or None | No | Regularization term: 'l2', 'l1', 'elasticnet', or None (default='l2') |
| alpha | float | No | Regularization strength constant (default=0.0001) |
| l1_ratio | float | No | Elastic Net mixing parameter; 0 for L2, 1 for L1 (default=0.15) |
| fit_intercept | bool | No | Whether to estimate the intercept (default=True) |
| max_iter | int | No | Maximum number of passes over the training data (default=1000) |
| tol | float | No | Stopping criterion tolerance (default=1e-3) |
| shuffle | bool | No | Whether to shuffle training data after each epoch (default=True) |
| learning_rate | str | No | Schedule: 'constant', 'optimal', 'invscaling', 'adaptive' (default='optimal') |
| eta0 | float | No | Initial learning rate for 'constant', 'invscaling', 'adaptive' schedules (default=0.0) |
| early_stopping | bool | No | Whether to use early stopping (default=False) |
| class_weight | dict or 'balanced' | No | Weights associated with classes (default=None) |
| warm_start | bool | No | Reuse previous solution as initialization (default=False) |
| average | bool or int | No | Whether to compute averaged SGD weights (default=False) |
Outputs
| Name | Type | Description |
|---|---|---|
| coef_ | ndarray of shape (1, n_features) or (n_classes, n_features) | Weights assigned to the features |
| intercept_ | ndarray of shape (1,) or (n_classes,) | Constants in the decision function |
| classes_ | ndarray of shape (n_classes,) | Unique class labels |
| n_iter_ | int | Number of iterations (epochs) performed |
| t_ | int | Number of weight updates performed during training |
Usage Examples
Basic Usage
from sklearn.linear_model import SGDClassifier
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
scaler = StandardScaler()
X = scaler.fit_transform(X)
model = SGDClassifier(loss="hinge", alpha=0.0001, max_iter=1000, random_state=42)
model.fit(X, y)
print("Score:", model.score(X, y))
print("Classes:", model.classes_)