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

Scikit-Learn · Class

LogisticRegression

LogisticRegression is a regularized classifier for binary and multiclass problems. It supports dense or sparse features and several solvers with different penalty and multiclass capabilities.

How it works

The estimator learns a regularized logistic decision function from feature matrix X and target y. Its selected solver optimizes the applicable binary or multinomial loss, while the regularization strength is controlled inversely by C; after fitting, it can predict labels, decision scores, and probabilities.

Syntax

sklearn.linear_model.LogisticRegression(penalty='l2', *, dual=False, tol=0.0001, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight=None, random_state=None, solver='lbfgs', max_iter=100, multi_class='deprecated', verbose=0, warm_start=False, n_jobs=None, l1_ratio=None)

Parameters

NameTypeDescription
Xarray-like or sparse matrixTraining feature matrix.
yarray-likeTarget values.
CfloatInverse of regularization strength; must be positive. Default: 1.0.
solver{'lbfgs', 'liblinear', 'newton-cg', 'newton-cholesky', 'sag', 'saga'}Algorithm used for optimization. Default: 'lbfgs'.
penalty{'l1', 'l2', 'elasticnet', None}Penalty specification. Default: 'l2'.
max_iterintMaximum number of iterations. Default: 100.
random_stateint, RandomState instance or NoneSeed used by solvers whose behavior involves randomness.

Returns

A fitted LogisticRegression estimator after fit; prediction methods return labels, decision scores, or class probabilities depending on the method used.

Key points

  • The class trains a logistic-regression classifier with multiple optimization backends.
  • A penalty is active unless configured otherwise, so the unregularized case must be requested explicitly.
  • Feature matrices may be supplied as dense arrays or sparse matrices.
  • With three or more classes, non-liblinear solvers use penalized multinomial loss.
  • These four solvers restrict the penalty to L2 or no penalty.
  • Liblinear supports L1 and L2, but its dual formulation is available only with L2.

Common mistakes

  • Choose saga when using an elastic-net penalty; other solvers do not support that combination.
  • Do not use liblinear directly for native multiclass optimization; wrap it with OneVsRestClassifier instead.
  • Prefer C-contiguous dense arrays or 64-bit CSR matrices to avoid conversion and copying.

Example: Fit and predict with logistic regression

from sklearn.linear_model import LogisticRegression

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

model = LogisticRegression(random_state=0).fit(X, y)
print(model.predict([[1, 1]]))
print(model.predict_proba([[1, 1]]))

Creates a regularized classifier, fits it to binary samples, then predicts a class and class probabilities.

When to use it

Class prediction and probabilities

Binary classification with calibrated class likelihoods

Sparse-feature classification

Sparse text or high-dimensional feature matrices

Native multiclass modeling

Multiclass classification using penalized multinomial loss

L1-regularized classification

Sparse feature selection with zeroed coefficients

Interview questions

QWhat does LogisticRegression implement?

It is a classifier that fits regularized logistic regression. Regularization is enabled by default, and the available solvers determine which penalty and multiclass formulation can be used.

Follow-up: Which solver supports elastic-net regularization? The documentation specifies saga only.

QHow does C affect the fitted model?

C is the inverse regularization-strength parameter: smaller values impose stronger regularization, while larger values impose weaker regularization. The parameter is positive and defaults to 1.0.

Follow-up: What should be checked after changing C? Verify that the chosen solver and penalty combination remains supported.

QHow does solver choice affect multiclass classification?

For at least three classes, every solver except liblinear optimizes penalized multinomial loss. Liblinear is limited to binary classification unless used inside OneVsRestClassifier.

Follow-up: How can liblinear be used for multiclass data? Wrap the estimator with OneVsRestClassifier to train one binary model per class.

  • LogisticRegressionCV
  • OneVsRestClassifier
  • SGDClassifier
  • RidgeClassifier

Source: LogisticRegression. Checked 2026-10-15.