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_results=False)[source]#

具有内置交叉验证的 Ridge 分类器。

参见词汇表条目 交叉验证估计器

默认情况下,它执行留一法交叉验证(Leave-One-Out Cross-Validation)。目前,仅高效处理了n_features > n_samples的情况。

用户指南中阅读更多内容。

参数:
alphas形状为 (n_alphas,) 的类数组,默认值=(0.1, 1.0, 10.0)

要尝试的 alpha 值数组。正则化强度;必须是正浮点数。正则化改善了问题的条件性并减少了估计的方差。值越大表示正则化越强。在其他线性模型(如 LogisticRegressionLinearSVC)中,Alpha 对应于 1 / (2C)。如果使用留一法交叉验证,alphas 必须严格为正。

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

用于交叉验证的评分方法。选项

  • str: 有关选项,请参阅 String name scorers

  • callable: 带有签名 scorer(estimator, X, y) 的可调用评分器对象(例如函数)。有关详细信息,请参阅 Callable scorers

  • None:如果 cv 为 None(即使用留一法交叉验证),则为负 均方误差,否则为 准确率

cvint, cross-validation generator or an iterable, default=None

确定交叉验证拆分策略。cv 的可能输入包括

  • 无,使用高效的留一法交叉验证

  • 整数,指定折数。

  • CV 分割器,

  • 一个可迭代对象,产生索引数组形式的 (训练集, 测试集) 拆分。

有关此处可使用的各种交叉验证策略,请参阅 用户指南

class_weightdict or ‘balanced’, default=None

{class_label: weight} 形式与类关联的权重。如果未给出,则所有类都被假定权重为一。

“balanced” 模式使用 y 的值根据输入数据中与类频率成反比的权重自动调整权重,计算公式为 n_samples / (n_classes * np.bincount(y))

store_cv_resultsbool, default=False

指示是否应将对应于每个 alpha 的交叉验证结果存储在 cv_results_ 属性中(见下文)的标志。此标志仅与 cv=None(即使用留一法交叉验证)兼容。

版本 1.5 中的变化:参数名称从 store_cv_values 更改为 store_cv_results

属性:
cv_results_形状为 (n_samples, n_targets, n_alphas) 的 ndarray,可选

每个 alpha 的交叉验证结果(仅当 store_cv_results=Truecv=None 时)。在调用 fit() 之后,如果 scoring is None,此属性将包含均方误差,否则将包含标准化后的每点预测值。

版本 1.5 中的变化:cv_values_ 更改为 cv_results_

coef_形状为 (1, n_features) 或 (n_targets, n_features) 的 ndarray

决策函数中的特征系数。

当给定的问题是二元问题时,coef_ 的形状为 (1, n_features)。

intercept_float or ndarray of shape (n_targets,)

决策函数中的独立项。如果fit_intercept = False,则设置为0.0。

alpha_float

估计的正则化参数。

best_score_float

具有最佳 alpha 的基础估计器的得分。

0.23 版本新增。

classes_ndarray of shape (n_classes,)

类别标签。

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

fit 期间看到的特征名称。仅当 X 具有全部为字符串的特征名称时才定义。

1.0 版本新增。

另请参阅

Ridge

岭回归。

RidgeClassifier

岭分类器。

RidgeCV

具有内置交叉验证的岭回归。

注意事项

对于多类别分类,采用一对多(one-versus-all)方法训练 n_class 个分类器。具体来说,这是通过利用 Ridge 中的多变量响应支持来实现的。

示例

>>> 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...
decision_function(X)[source]#

预测样本的置信度得分。

样本的置信度得分与该样本到超平面的有符号距离成比例。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

我们想要获取置信度得分的数据矩阵。

返回:
scoresndarray of shape (n_samples,) or (n_samples, n_classes)

每个 (n_samples, n_classes) 组合的置信度得分。在二元情况下,self.classes_[1] 的置信度得分大于 0 意味着将预测此类。

fit(X, y, sample_weight=None, **params)[source]#

使用 cv 拟合岭分类器。

参数:
Xndarray of shape (n_samples, n_features)

训练向量,其中 n_samples 是样本数,n_features 是特征数。如果需要,使用 GCV 时将转换为 float64。

yndarray of shape (n_samples,)

目标值。如有必要,将被转换为 X 的 dtype。

sample_weightfloat or ndarray of shape (n_samples,), default=None

每个样本的单独权重。如果给定一个浮点数,每个样本将具有相同的权重。

**paramsdict, default=None

要传递给底层评分器的参数。

Added in version 1.5: Only available if enable_metadata_routing=True, which can be set by using sklearn.set_config(enable_metadata_routing=True). See Metadata Routing User Guide for more details.

返回:
selfobject

拟合的估计器。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查阅 用户指南,了解路由机制如何工作。

1.5 版本新增。

返回:
routingMetadataRouter

封装路由信息的 MetadataRouter

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。

返回:
paramsdict

参数名称映射到其值。

predict(X)[source]#

预测 X 中样本的类别标签。

参数:
X形状为 (n_samples, n_features) 的 {类数组, 稀疏矩阵}

我们想要预测目标的矩阵数据。

返回:
y_pred形状为 (n_samples,) 或 (n_samples, n_outputs) 的 ndarray

包含预测的向量或矩阵。在二元和多类别问题中,这是一个包含 n_samples 的向量。在多标签问题中,它返回一个形状为 (n_samples, n_outputs) 的矩阵。

score(X, y, sample_weight=None)[source]#

返回在提供的数据和标签上的 准确率 (accuracy)

在多标签分类中,这是子集准确率 (subset accuracy),这是一个严格的指标,因为它要求每个样本的每个标签集都被正确预测。

参数:
Xshape 为 (n_samples, n_features) 的 array-like

测试样本。

yshape 为 (n_samples,) 或 (n_samples, n_outputs) 的 array-like

X 的真实标签。

sample_weightshape 为 (n_samples,) 的 array-like, default=None

样本权重。

返回:
scorefloat

self.predict(X) 相对于 y 的平均准确率。

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RidgeClassifierCV[source]#

配置是否应请求元数据以传递给 fit 方法。

请注意,此方法仅在以下情况下相关:此估计器用作 元估计器 中的子估计器,并且通过 enable_metadata_routing=True 启用了元数据路由(请参阅 sklearn.set_config)。请查看 用户指南 以了解路由机制的工作原理。

每个参数的选项如下:

  • True:请求元数据,如果提供则传递给 fit。如果未提供元数据,则忽略该请求。

  • False:不请求元数据,元估计器不会将其传递给 fit

  • None:不请求元数据,如果用户提供元数据,元估计器将引发错误。

  • str:应将元数据以给定别名而不是原始名称传递给元估计器。

默认值 (sklearn.utils.metadata_routing.UNCHANGED) 保留现有请求。这允许您更改某些参数的请求而不更改其他参数。

在版本 1.3 中新增。

参数:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

fit 方法中 sample_weight 参数的元数据路由。

返回:
selfobject

更新后的对象。

set_params(**params)[source]#

设置此估计器的参数。

此方法适用于简单的估计器以及嵌套对象(如 Pipeline)。后者具有 <component>__<parameter> 形式的参数,以便可以更新嵌套对象的每个组件。

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RidgeClassifierCV[source]#

配置是否应请求元数据以传递给 score 方法。

请注意,此方法仅在以下情况下相关:此估计器用作 元估计器 中的子估计器,并且通过 enable_metadata_routing=True 启用了元数据路由(请参阅 sklearn.set_config)。请查看 用户指南 以了解路由机制的工作原理。

每个参数的选项如下:

  • True:请求元数据,如果提供则传递给 score。如果未提供元数据,则忽略该请求。

  • False:不请求元数据,元估计器不会将其传递给 score

  • None:不请求元数据,如果用户提供元数据,元估计器将引发错误。

  • str:应将元数据以给定别名而不是原始名称传递给元估计器。

默认值 (sklearn.utils.metadata_routing.UNCHANGED) 保留现有请求。这允许您更改某些参数的请求而不更改其他参数。

在版本 1.3 中新增。

参数:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

score 方法中 sample_weight 参数的元数据路由。

返回:
selfobject

更新后的对象。