LarsCV#

class sklearn.linear_model.LarsCV(*, fit_intercept=True, verbose=False, max_iter=500, precompute='auto', cv=None, max_n_alphas=1000, n_jobs=None, eps=np.float64(2.220446049250313e-16), copy_X=True)[source]#

交叉验证的最小角回归模型。

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

Read more in the User Guide.

参数:
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).

verbosebool or int, default=False

Sets the verbosity amount.

max_iterint, default=500

要执行的最大迭代次数。

precomputebool, ‘auto’ or array-like , default=’auto’

Whether to use a precomputed Gram matrix to speed up calculations. If set to 'auto' let us decide. The Gram matrix cannot be passed as argument since we will use only subsets of X.

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

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

  • None,使用默认的 5 折交叉验证,

  • 整数,指定折数。

  • CV 分割器,

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

For integer/None inputs, KFold is used.

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

版本 0.22 中已更改:如果为 None,cv 默认值从 3 折更改为 5 折。

max_n_alphasint, default=1000

The maximum number of points on the path used to compute the residuals in the cross-validation.

n_jobsint or None, default=None

Number of CPUs to use during the cross validation. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.

epsfloat, default=np.finfo(float).eps

The machine-precision regularization in the computation of the Cholesky diagonal factors. Increase this for very ill-conditioned systems. Unlike the tol parameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization.

copy_Xbool, default=True

如果为True,X将被复制;否则,它可能会被覆盖。

属性:
active_list of length n_alphas or list of such lists

Indices of active variables at the end of the path. If this is a list of lists, the outer list length is n_targets.

coef_array-like of shape (n_features,)

parameter vector (w in the formulation formula)

intercept_float

independent term in decision function

coef_path_array-like of shape (n_features, n_alphas)

the varying values of the coefficients along the path

alpha_float

the estimated regularization parameter alpha

alphas_array-like of shape (n_alphas,)

the different values of alpha along the path

cv_alphas_array-like of shape (n_cv_alphas,)

all the values of alpha along the path for the different folds

mse_path_array-like of shape (n_folds, n_cv_alphas)

the mean square error on left-out for each fold along the path (alpha values given by cv_alphas)

n_iter_array-like or int

the number of iterations run by Lars with the optimal alpha.

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

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

1.0 版本新增。

另请参阅

lars_path

Compute Least Angle Regression or Lasso path using LARS algorithm.

lasso_path

使用坐标下降计算 Lasso 路径。

Lasso

使用 L1 先验作为正则化项训练的线性模型(又名 Lasso)。

LassoCV

具有沿正则化路径迭代拟合的 Lasso 线性模型。

LassoLars

使用最小角回归(又名 Lars)拟合的 Lasso 模型。

LassoLarsIC

使用 BIC 或 AIC 进行模型选择的 Lars 拟合 Lasso 模型。

sklearn.decomposition.sparse_encode

稀疏编码。

注意事项

In fit, once the best parameter alpha is found through cross-validation, the model is fit again using the entire training set.

示例

>>> from sklearn.linear_model import LarsCV
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_samples=200, noise=4.0, random_state=0)
>>> reg = LarsCV(cv=5).fit(X, y)
>>> reg.score(X, y)
0.9996
>>> reg.alpha_
np.float64(0.2961)
>>> reg.predict(X[:1,])
array([154.3996])
fit(X, y, **params)[source]#

Fit the model using X, y as training data.

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

训练数据。

yarray-like of shape (n_samples,)

目标值。

**paramsdict, default=None

Parameters to be passed to the CV splitter.

Added in version 1.4: 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

Returns an instance of self.

get_metadata_routing()[source]#

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

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

1.4 版本新增。

返回:
routingMetadataRouter

封装路由信息的 MetadataRouter

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

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

返回:
paramsdict

参数名称映射到其值。

predict(X)[source]#

使用线性模型进行预测。

参数:
Xarray-like or sparse matrix, shape (n_samples, n_features)

样本。

返回:
Carray, shape (n_samples,)

返回预测值。

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

返回测试数据的 决定系数

The coefficient of determination, \(R^2\), is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)** 2).sum() and \(v\) is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.

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

测试样本。对于某些估计器,这可能是一个预先计算的核矩阵或一个通用对象列表,形状为 (n_samples, n_samples_fitted),其中 n_samples_fitted 是用于估计器拟合的样本数。

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

X 的真实值。

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

样本权重。

返回:
scorefloat

self.predict(X) 相对于 y\(R^2\)

注意事项

The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

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

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

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

每个参数的选项如下:

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

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

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

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

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

在版本 1.3 中新增。

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

Metadata routing for Xy parameter in fit.

返回:
selfobject

更新后的对象。

set_params(**params)[source]#

设置此估计器的参数。

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

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') LarsCV[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

更新后的对象。