Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

SGDClassifier

SGDClassifier trains regularized linear classifiers with stochastic gradient descent. It supports configurable losses, sparse or dense floating-point features, and incremental learning through partial_fit.

How it works

SGDClassifier fits a regularized linear classification model by estimating each sample's loss gradient and updating parameters using a decreasing learning-rate schedule. The selected loss determines the model behavior; partial_fit supports incremental or out-of-core training, while suitable feature scaling can improve results with the default schedule.

Syntax

SGDClassifier(loss='hinge', *, penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, max_iter=1000, tol=0.001, shuffle=True, verbose=0, epsilon=0.1, 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)

Parameters

NameTypeDescription
lossstr, default='hinge'The loss function to use; the default is hinge.
penaltystr, default='l2'The penalty specification used for regularization.
max_iterint, default=1000The maximum number of passes over the training data for fit.
learning_ratestr, default='optimal'The learning-rate schedule.
alphafloat, default=0.0001The coefficient used to multiply the regularization term.
averagebool or int, default=FalseWhether to use averaged SGD.
fit_interceptbool, default=TrueWhether to fit the intercept.
random_stateint, RandomState instance or None, default=NoneThe random seed used for shuffling and reproducibility.

Returns

An SGDClassifier estimator object.

Key points

  • Use it for linear classification tasks, including SVM-like and logistic-regression-like models.
  • Training estimates the loss gradient per sample rather than computing a full-batch gradient.
  • Parameters are updated progressively while the learning-rate strength decreases.
  • Use partial_fit to process data incrementally or in minibatches.
  • Feature matrices may be dense or sparse, provided feature values are floating point.
  • Choose the classification model behavior through the loss setting.
  • Enable averaged stochastic gradient descent with average=True when appropriate.

Common mistakes

  • Standardize features when using the default learning-rate schedule to improve optimization.

Example: Train and predict with SGDClassifier

from sklearn.linear_model import SGDClassifier

X = [[0., 0.], [1., 1.], [1., 0.], [0., 1.]]
y = [0, 1, 1, 0]

clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=1000, random_state=0)
clf.fit(X, y)
print(clf.predict([[0.8, 0.8]]))

Creates a regularized linear classifier, trains it on labeled samples, and predicts a class for a new sample.

When to use it

Minibatch or out-of-core classification

Large labeled datasets that can be processed in chunks.

Sparse linear classification

Sparse feature representations such as high-dimensional text features.

Loss-configurable linear models

Tasks needing different linear classification objectives.

Averaged SGD

Models where a lower-variance online estimate is useful.

Interview questions

QHow does SGDClassifier train a model?

It is a linear classifier trained with regularized stochastic gradient descent. The gradient is estimated one sample at a time, and the parameters are updated under a decreasing learning-rate schedule.

Follow-up: How would you train when the full dataset cannot fit in memory? Use partial_fit for minibatch or out-of-core updates.

QWhat does the loss parameter control?

The loss parameter selects the classification objective; with the default loss, the estimator fits a linear support vector machine. Other supported losses can produce different linear-model behavior.

Follow-up: What is one practical choice for probability-like outputs? Select a probabilistic loss such as log_loss, then use the estimator's probability methods when supported.

QWhat is the difference between fit and partial_fit?

fit trains for up to max_iter passes over the training data. partial_fit is intended for incremental updates and is not governed by max_iter.

Follow-up: What preprocessing is especially useful with the default schedule? Scale features to approximately zero mean and unit variance.

  • SGDRegressor
  • LogisticRegression
  • LinearSVC
  • sklearn.linear_model

Source: SGDClassifier — scikit-learn 1.9.1 documentation. Checked 2026-10-15.