>>> from sklearn.datasets import load_iris
>>> from sklearn.linear_model import LogisticRegression
>>> X, y = load_iris(return_X_y=True)
>>> clf = LogisticRegression(random_state=0).fit(X, y)
>>> clf.predict(X[:2, :])
array([0, 0])
>>> clf.predict_proba(X[:2, :])
array([[9.82e-01, 1.82e-02, 1.44e-08],
[9.72e-01, 2.82e-02, 3.02e-08]])
>>> clf.score(X, y)
For a comparison of the LogisticRegression with other classifiers see:
Plot classification probability.
decision_function(X)[source]
Predict confidence scores for samples.
The confidence score for a sample is proportional to the signed
distance of that sample to the hyperplane.
Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)The data matrix for which we want to get the confidence scores.
Returns:
scoresndarray of shape (n_samples,) or (n_samples, n_classes)Confidence scores per (n_samples, n_classes)
combination. In the
binary case, confidence score for self.classes_[1]
where >0 means
this class would be predicted.
densify()[source]
Convert coefficient matrix to dense array format.
Converts the coef_
member (back) to a numpy.ndarray. This is the
default format of coef_
and is required for fitting, so calling
this method is only required on models that have previously been
sparsified; otherwise, it is a no-op.
Returns:
selfFitted estimator.
fit(X, y, sample_weight=None)[source]
Fit the model according to the given training data.
Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)Training vector, where n_samples
is the number of samples and
n_features
is the number of features.
yarray-like of shape (n_samples,)Target vector relative to X.
sample_weightarray-like of shape (n_samples,) default=NoneArray of weights that are assigned to individual samples.
If not provided, then each sample is given unit weight.
Added in version 0.17: sample_weight support to LogisticRegression.
get_metadata_routing()[source]
Get metadata routing of this object.
Please check User Guide on how the routing
mechanism works.
Returns:
routingMetadataRequestA MetadataRequest
encapsulating
routing information.
Parameters:
deepbool, default=TrueIf True, will return the parameters for this estimator and
contained subobjects that are estimators.
Returns:
paramsdictParameter names mapped to their values.
predict_log_proba(X)[source]
Predict logarithm of probability estimates.
The returned estimates for all classes are ordered by the
label of classes.
Parameters:
Xarray-like of shape (n_samples, n_features)Vector to be scored, where n_samples
is the number of samples and
n_features
is the number of features.
Returns:
Tarray-like of shape (n_samples, n_classes)Returns the log-probability of the sample for each class in the
model, where classes are ordered as they are in self.classes_
.
predict_proba(X)[source]
Probability estimates.
The returned estimates for all classes are ordered by the
label of classes.
For a multi_class problem, if multi_class is set to be “multinomial”
the softmax function is used to find the predicted probability of
each class.
Else use a one-vs-rest approach, i.e. calculate the probability
of each class assuming it to be positive using the logistic function
and normalize these values across all the classes.
Parameters:
Xarray-like of shape (n_samples, n_features)Vector to be scored, where n_samples
is the number of samples and
n_features
is the number of features.
Returns:
Tarray-like of shape (n_samples, n_classes)Returns the probability of the sample for each class in the model,
where classes are ordered as they are in self.classes_
.
score(X, y, sample_weight=None)[source]
Return accuracy on provided data and labels.
In multi-label classification, this is the subset accuracy
which is a harsh metric since you require for each sample that
each label set be correctly predicted.
Parameters:
Xarray-like of shape (n_samples, n_features)Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)True labels for X
.
sample_weightarray-like of shape (n_samples,), default=NoneSample weights.
Returns:
scorefloatMean accuracy of self.predict(X)
w.r.t. y
.
set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → LogisticRegression[source]
Request metadata passed to the fit
method.
Note that this method is only relevant if
enable_metadata_routing=True
(see sklearn.set_config
).
Please see User Guide on how the routing
mechanism works.
The options for each parameter are:
True
: metadata is requested, and passed to fit
if provided. The request is ignored if metadata is not provided.
False
: metadata is not requested and the meta-estimator will not pass it to fit
.
None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED
) retains the
existing request. This allows you to change the request for some
parameters and not others.
Added in version 1.3.
This method is only relevant if this estimator is used as a
sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.
Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGEDMetadata routing for sample_weight
parameter in fit
.
Returns:
selfobjectThe updated object.
set_params(**params)[source]
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline
). The latter have
parameters of the form <component>__<parameter>
so that it’s
possible to update each component of a nested object.
Parameters:
**paramsdictEstimator parameters.
Returns:
selfestimator instanceEstimator instance.
set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → LogisticRegression[source]
Request metadata passed to the score
method.
Note that this method is only relevant if
enable_metadata_routing=True
(see sklearn.set_config
).
Please see User Guide on how the routing
mechanism works.
The options for each parameter are:
True
: metadata is requested, and passed to score
if provided. The request is ignored if metadata is not provided.
False
: metadata is not requested and the meta-estimator will not pass it to score
.
None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED
) retains the
existing request. This allows you to change the request for some
parameters and not others.
Added in version 1.3.
This method is only relevant if this estimator is used as a
sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.
Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGEDMetadata routing for sample_weight
parameter in score
.
Returns:
selfobjectThe updated object.
sparsify()[source]
Convert coefficient matrix to sparse format.
Converts the coef_
member to a scipy.sparse matrix, which for
L1-regularized models can be much more memory- and storage-efficient
than the usual numpy.ndarray representation.
The intercept_
member is not converted.
Returns:
selfFitted estimator.
Notes
For non-sparse models, i.e. when there are not many zeros in coef_
,
this may actually increase memory usage, so use this method with
care. A rule of thumb is that the number of zero elements, which can
be computed with (coef_ == 0).sum()
, must be more than 50% for this
to provide significant benefits.
After calling this method, further fitting with the partial_fit
method (if any) will not work until you call densify.
Pipelining: chaining a PCA and a logistic regression
Pipelining: chaining a PCA and a logistic regression
Feature transformations with ensembles of trees
Feature transformations with ensembles of trees
Visualizing the probabilistic predictions of a VotingClassifier
Visualizing the probabilistic predictions of a VotingClassifier
Recursive feature elimination
Recursive feature elimination
Recursive feature elimination with cross-validation
Recursive feature elimination with cross-validation
Model-based and sequential feature selection
Model-based and sequential feature selection
Examples of Using FrozenEstimator
Examples of Using FrozenEstimator
Logistic function
Logistic function
L1 Penalty and Sparsity in Logistic Regression
L1 Penalty and Sparsity in Logistic Regression
Decision Boundaries of Multinomial and One-vs-Rest Logistic Regression
Decision Boundaries of Multinomial and One-vs-Rest Logistic Regression
Regularization path of L1- Logistic Regression
Regularization path of L1- Logistic Regression
Multiclass sparse logistic regression on 20newgroups
Multiclass sparse logistic regression on 20newgroups
MNIST classification using multinomial logistic + L1
MNIST classification using multinomial logistic + L1
Visualizations with Display Objects
Visualizations with Display Objects
Displaying estimators and complex pipelines
Displaying estimators and complex pipelines
Displaying Pipelines
Displaying Pipelines
Introducing the set_output API
Introducing the set_output API
Post-tuning the decision threshold for cost-sensitive learning
Post-tuning the decision threshold for cost-sensitive learning
Balance model complexity and cross-validated score
Balance model complexity and cross-validated score
Class Likelihood Ratios to measure classification performance
Class Likelihood Ratios to measure classification performance
Multiclass Receiver Operating Characteristic (ROC)
Multiclass Receiver Operating Characteristic (ROC)
Post-hoc tuning the cut-off point of decision function
Post-hoc tuning the cut-off point of decision function
Multilabel classification using a classifier chain
Multilabel classification using a classifier chain
Restricted Boltzmann Machine features for digit classification
Restricted Boltzmann Machine features for digit classification
Feature discretization
Feature discretization
Release Highlights for scikit-learn 0.22
Release Highlights for scikit-learn 0.22
Release Highlights for scikit-learn 0.23
Release Highlights for scikit-learn 0.23
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 0.24
Release Highlights for scikit-learn 1.0
Release Highlights for scikit-learn 1.0
Release Highlights for scikit-learn 1.1
Release Highlights for scikit-learn 1.1
Release Highlights for scikit-learn 1.3
Release Highlights for scikit-learn 1.3
Release Highlights for scikit-learn 1.5
Release Highlights for scikit-learn 1.5
Release Highlights for scikit-learn 1.7
Release Highlights for scikit-learn 1.7
Classification of text documents using sparse features
Classification of text documents using sparse features