sklearn.linear_model
.RidgeClassifierCV¶
- class sklearn.linear_model.RidgeClassifierCV(alphas=(0.1, 1.0, 10.0), *, fit_intercept=True, scoring=None, cv=None, class_weight=None, store_cv_values=False)[source]¶
Ridge classifier with built-in cross-validation.
See glossary entry for cross-validation estimator.
By default, it performs Leave-One-Out Cross-Validation. Currently, only the n_features > n_samples case is handled efficiently.
Read more in the User Guide.
- Parameters:
- alphasarray-like of shape (n_alphas,), default=(0.1, 1.0, 10.0)
Array of alpha values to try. Regularization strength; must be a positive float. Regularization improves the conditioning of the problem and reduces the variance of the estimates. Larger values specify stronger regularization. Alpha corresponds to
1 / (2C)
in other linear models such asLogisticRegression
orLinearSVC
.- fit_interceptbool, default=True
Whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be centered).
- scoringstr, callable, default=None
A string (see model evaluation documentation) or a scorer callable object / function with signature
scorer(estimator, X, y)
.- cvint, cross-validation generator or an iterable, default=None
Determines the cross-validation splitting strategy. Possible inputs for cv are:
None, to use the efficient Leave-One-Out cross-validation
integer, to specify the number of folds.
An iterable yielding (train, test) splits as arrays of indices.
Refer User Guide for the various cross-validation strategies that can be used here.
- class_weightdict or ‘balanced’, default=None
Weights associated with classes in the form
{class_label: weight}
. If not given, all classes are supposed to have weight one.The “balanced” mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as
n_samples / (n_classes * np.bincount(y))
.- store_cv_valuesbool, default=False
Flag indicating if the cross-validation values corresponding to each alpha should be stored in the
cv_values_
attribute (see below). This flag is only compatible withcv=None
(i.e. using Leave-One-Out Cross-Validation).
- Attributes:
- cv_values_ndarray of shape (n_samples, n_targets, n_alphas), optional
Cross-validation values for each alpha (only if
store_cv_values=True
andcv=None
). Afterfit()
has been called, this attribute will contain the mean squared errors ifscoring is None
otherwise it will contain standardized per point prediction values.- coef_ndarray of shape (1, n_features) or (n_targets, n_features)
Coefficient of the features in the decision function.
coef_
is of shape (1, n_features) when the given problem is binary.- intercept_float or ndarray of shape (n_targets,)
Independent term in decision function. Set to 0.0 if
fit_intercept = False
.- alpha_float
Estimated regularization parameter.
- best_score_float
Score of base estimator with best alpha.
New in version 0.23.
classes_
ndarray of shape (n_classes,)Classes labels.
- n_features_in_int
Number of features seen during fit.
New in version 0.24.
- feature_names_in_ndarray of shape (
n_features_in_
,) Names of features seen during fit. Defined only when
X
has feature names that are all strings.New in version 1.0.
See also
Ridge
Ridge regression.
RidgeClassifier
Ridge classifier.
RidgeCV
Ridge regression with built-in cross validation.
Notes
For multi-class classification, n_class classifiers are trained in a one-versus-all approach. Concretely, this is implemented by taking advantage of the multi-variate response support in Ridge.
Examples
>>> from sklearn.datasets import load_breast_cancer >>> from sklearn.linear_model import RidgeClassifierCV >>> X, y = load_breast_cancer(return_X_y=True) >>> clf = RidgeClassifierCV(alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X, y) >>> clf.score(X, y) 0.9630...
Methods
Predict confidence scores for samples.
fit
(X, y[, sample_weight])Fit Ridge classifier with cv.
Raise
NotImplementedError
.get_params
([deep])Get parameters for this estimator.
predict
(X)Predict class labels for samples in
X
.score
(X, y[, sample_weight])Return the mean accuracy on the given test data and labels.
set_fit_request
(*[, sample_weight])Request metadata passed to the
fit
method.set_params
(**params)Set the parameters of this estimator.
set_score_request
(*[, sample_weight])Request metadata passed to the
score
method.- property classes_¶
Classes labels.
- 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 forself.classes_[1]
where >0 means this class would be predicted.
- fit(X, y, sample_weight=None)[source]¶
Fit Ridge classifier with cv.
- Parameters:
- Xndarray of shape (n_samples, n_features)
Training vectors, where
n_samples
is the number of samples andn_features
is the number of features. When using GCV, will be cast to float64 if necessary.- yndarray of shape (n_samples,)
Target values. Will be cast to X’s dtype if necessary.
- sample_weightfloat or ndarray of shape (n_samples,), default=None
Individual weights for each sample. If given a float, every sample will have the same weight.
- Returns:
- selfobject
Fitted estimator.
- get_metadata_routing()[source]¶
Raise
NotImplementedError
.This estimator does not support metadata routing yet.
- get_params(deep=True)[source]¶
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- predict(X)[source]¶
Predict class labels for samples in
X
.- Parameters:
- X{array-like, spare matrix} of shape (n_samples, n_features)
The data matrix for which we want to predict the targets.
- Returns:
- y_predndarray of shape (n_samples,) or (n_samples, n_outputs)
Vector or matrix containing the predictions. In binary and multiclass problems, this is a vector containing
n_samples
. In a multilabel problem, it returns a matrix of shape(n_samples, n_outputs)
.
- score(X, y, sample_weight=None)[source]¶
Return the mean accuracy on the given test 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=None
Sample weights.
- Returns:
- scorefloat
Mean accuracy of
self.predict(X)
w.r.t.y
.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RidgeClassifierCV [source]¶
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.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.New in version 1.3.
Note
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.UNCHANGED
Metadata routing for
sample_weight
parameter infit
.
- Returns:
- selfobject
The 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:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RidgeClassifierCV [source]¶
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.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.New in version 1.3.
Note
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.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns:
- selfobject
The updated object.