RidgeCV#
- class sklearn.linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0), *, fit_intercept=True, scoring=None, cv=None, gcv_mode=None, store_cv_results=None, alpha_per_target=False, store_cv_values='deprecated')#
岭回归内置交叉验证。
请参阅 cross-validation estimator 的词汇条目。
默认情况下,它执行高效的留一交叉验证。
更多信息请参阅 User Guide 。
- Parameters:
- alphasarray-like of shape (n_alphas,), default=(0.1, 1.0, 10.0)
要尝试的 alpha 值数组。 正则化强度;必须是正浮点数。正则化改善了问题的条件并减少了估计的方差。 较大的值指定更强的正则化。 Alpha 对应于其他线性模型中的
1 / (2C)
,例如LogisticRegression
或LinearSVC
。 如果使用留一交叉验证,alpha 必须严格为正。- fit_interceptbool, default=True
是否计算此模型的截距。如果设置为 false,则不会在计算中使用截距 (即数据应已中心化)。
- scoringstr, callable, default=None
一个字符串(参见 scoring_parameter )或带有签名
scorer(estimator, X, y)
的评分器可调用对象 / 函数。如果为 None, 如果 cv 为 ‘auto’ 或 None(即使用留一交叉验证时),则为负均方误差,否则为 r2 评分。- cvint, cross-validation generator or an iterable, default=None
确定交叉验证分割策略。 cv 的可能输入包括:
None,使用高效的留一交叉验证
整数,指定折数。
一个生成 (train, test) 分割为索引数组的迭代器。
对于整数/None 输入,如果
y
是二分类或多分类, 使用StratifiedKFold
,否则, 使用KFold
。请参阅 User Guide 以了解可以使用的各种交叉验证策略。
- gcv_mode{‘auto’, ‘svd’, ‘eigen’}, default=’auto’
指示在执行留一交叉验证时使用哪种策略的标志。选项包括:
‘auto’ : 如果 n_samples > n_features,则使用 ‘svd’,否则使用 ‘eigen’ ‘svd’ : 当 X 为稠密时,强制使用 X 的奇异值分解,当 X 为稀疏时,强制使用 X^T.X 的特征值分解。 ‘eigen’ : 强制通过 X.X^T 的特征分解进行计算
‘auto’ 模式是默认模式,旨在根据训练数据的形状选择较便宜的选项。
- store_cv_resultsbool, default=False
指示是否应将每个 alpha 对应的交叉验证值存储在
cv_values_
属性中(见下文)。此标志仅与cv=None
(即使用留一交叉验证)兼容。Changed in version 1.5: 参数名称从
store_cv_values
更改为store_cv_results
。- alpha_per_targetbool, default=False
指示是否应为每个目标单独优化 alpha 值(从
alphas
参数列表中选择)(对于多输出设置:多个预测目标)。当设置为True
时,在拟合后,alpha_
属性将包含每个目标的值。当设置为False
时,所有目标使用单个 alpha。Added in version 0.24.
- store_cv_valuesbool
指示是否应将每个 alpha 对应的交叉验证值存储在
cv_values_
属性中(见下文)。此标志仅与cv=None
(即使用留一交叉验证)兼容。Deprecated since version 1.5:
store_cv_values
在版本 1.5 中已弃用,取而代之的是store_cv_results
,并将在版本 1.7 中移除。
- Attributes:
- cv_results_ndarray of shape (n_samples, n_alphas) or shape (n_samples, n_targets, n_alphas), optional
每个 alpha 的交叉验证值(仅在
store_cv_results=True
且cv=None
时可用)。调用fit()
后,此属性将包含均方误差(如果scoring is None
),否则将包含标准化每点预测值。Changed in version 1.5:
cv_values_
更改为cv_results_
。- coef_ndarray of shape (n_features) or (n_targets, n_features)
权重向量。
- intercept_float or ndarray of shape (n_targets,)
决策函数中的独立项。如果
fit_intercept = False
,则设置为 0.0。- alpha_float or ndarray of shape (n_targets,)
估计的正则化参数,或者,如果
alpha_per_target=True
,则为每个目标估计的正则化参数。- best_score_float or ndarray of shape (n_targets,)
具有最佳 alpha 的基础估计器的分数,或者,如果
alpha_per_target=True
,则为每个目标的分数。Added in version 0.23.
- n_features_in_int
在 fit 期间看到的特征数。
Added in version 0.24.
- feature_names_in_ndarray of shape (
n_features_in_
,) 在 fit 期间看到的特征名称。仅当
X
的特征名称为所有字符串时定义。Added in version 1.0.
See also
Ridge
岭回归。
RidgeClassifier
基于 {-1, 1} 标签的岭回归分类器。
RidgeClassifierCV
内置交叉验证的岭分类器。
Examples
>>> from sklearn.datasets import load_diabetes >>> from sklearn.linear_model import RidgeCV >>> X, y = load_diabetes(return_X_y=True) >>> clf = RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X, y) >>> clf.score(X, y) 0.5166...
- fit(X, y, sample_weight=None, **params)#
拟合带有交叉验证的岭回归模型。
- Parameters:
- X形状为 (n_samples, n_features) 的 ndarray
训练数据。如果使用 GCV,必要时将被转换为 float64。
- y形状为 (n_samples,) 或 (n_samples, n_targets) 的 ndarray
目标值。必要时将被转换为 X 的数据类型。
- sample_weightfloat 或 形状为 (n_samples,) 的 ndarray, 默认=None
每个样本的个体权重。如果给定一个浮点数,每个样本将有相同的权重。
- **paramsdict, 默认=None
要传递给底层评分器的参数。
Added in version 1.5: 仅在
enable_metadata_routing=True
时可用, 可以通过使用sklearn.set_config(enable_metadata_routing=True)
设置。 有关更多详细信息,请参阅 Metadata Routing 用户指南 。
- Returns:
- selfobject
拟合的估计器。
Notes
当提供 sample_weight 时,所选的超参数可能取决于我们使用的是留一交叉验证(cv=None 或 cv=’auto’) 还是其他形式的交叉验证,因为只有留一交叉验证在计算验证分数时考虑了样本权重。
- get_metadata_routing()#
获取此对象的元数据路由。
请查看 用户指南 以了解路由机制的工作原理。
Added in version 1.5.
- Returns:
- routingMetadataRouter
MetadataRouter
封装的 路由信息。
- get_params(deep=True)#
获取此估计器的参数。
- Parameters:
- deepbool, 默认=True
如果为True,将返回此估计器和包含的子对象(也是估计器)的参数。
- Returns:
- paramsdict
参数名称映射到它们的值。
- predict(X)#
使用线性模型进行预测。
- Parameters:
- Xarray-like 或 sparse matrix, shape (n_samples, n_features)
样本。
- Returns:
- Carray, shape (n_samples,)
返回预测值。
- score(X, y, sample_weight=None)#
返回预测的决定系数。
决定系数 \(R^2\) 定义为 \((1 - rac{u}{v})\) ,其中 \(u\) 是残差平方和
((y_true - y_pred)** 2).sum()
,而 \(v\) 是总平方和((y_true - y_true.mean()) ** 2).sum()
。最好的可能得分是 1.0,它可能是负的(因为模型可能任意地差)。一个总是预测y
的期望值的常数模型,忽略输入特征,将得到 \(R^2\) 得分为 0.0。- Parameters:
- Xarray-like of shape (n_samples, n_features)
测试样本。对于某些估计器,这可能是一个预计算的核矩阵或一个形状为
(n_samples, n_samples_fitted)
的通用对象列表,其中n_samples_fitted
是估计器拟合中使用的样本数量。- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
X
的真实值。- sample_weightarray-like of shape (n_samples,), default=None
样本权重。
- Returns:
- scorefloat
\(R^2\) 相对于
y
的self.predict(X)
。
Notes
在调用回归器的
score
时使用的 \(R^2\) 得分从 0.23 版本开始使用multioutput='uniform_average'
以保持与r2_score
默认值一致。 这影响了所有多输出回归器的score
方法(除了MultiOutputRegressor
)。
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RidgeCV #
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.Added 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)#
设置此估计器的参数。
该方法适用于简单估计器以及嵌套对象(例如
Pipeline
)。后者具有形式为<component>__<parameter>
的参数,以便可以更新嵌套对象的每个组件。- Parameters:
- **paramsdict
估计器参数。
- Returns:
- selfestimator instance
估计器实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RidgeCV #
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.Added 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.