Lars#
- class sklearn.linear_model.Lars(*, fit_intercept=True, verbose=False, precompute='auto', n_nonzero_coefs=500, eps=np.float64(2.220446049250313e-16), copy_X=True, fit_path=True, jitter=None, random_state=None)[source]#
最小角回归模型,又名 LAR。
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.
- precomputebool, ‘auto’ 或 array-like , 默认值='auto'
是否使用预计算的Gram矩阵来加快计算速度。如果设置为
'auto',则由我们决定。Gram矩阵也可以作为参数传入。- n_nonzero_coefsint, 默认值=500
非零系数的目标数量。使用
np.inf表示没有限制。- 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
tolparameter in some iterative optimization-based algorithms, this parameter does not control the tolerance of the optimization.- copy_Xbool, default=True
如果为
True,X将被复制;否则,它可能会被覆盖。- fit_pathbool, 默认值=True
如果为 True,则完整路径存储在
coef_path_属性中。如果你要计算一个大型问题或许多目标的解决方案,将fit_path设置为False将会提高速度,尤其是在 alpha 值较小的情况下。- jitterfloat, 默认值=None
要添加到
y值的均匀噪声参数的上限,以满足模型一次一计算的假设。可能有助于稳定性。0.23 版本新增。
- random_stateint, RandomState instance or None, default=None
确定抖动(jittering)的随机数生成。传入一个整数可以在多次函数调用中获得可重现的输出。请参阅 Glossary。如果
jitter为 None 则忽略。0.23 版本新增。
- 属性:
- alphas_array-like of shape (n_alphas + 1,) or list of such arrays
每次迭代中协方差(绝对值)的最大值。
n_alphas是max_iter、n_features或路径中alpha >= alpha_min的节点数量中的较小者。如果这是一个类数组列表,则外部列表的长度为n_targets。- active_形状为 (n_alphas,) 的列表或此类列表的列表
路径末端活跃变量的索引。如果这是一个列表的列表,则外部列表的长度为
n_targets。- coef_path_形状为 (n_features, n_alphas + 1) 的类数组或此类数组的列表
沿路径变化的系数的值。如果
fit_path参数为False,则不显示此项。如果这是一个类数组列表,则外部列表的长度为n_targets。- coef_形状为 (n_features,) 或 (n_targets, n_features) 的类数组
参数向量(公式中的 w)。
- intercept_float 或 形状为 (n_targets,) 的类数组
决策函数中的独立项。
- n_iter_array-like or int
lars_path 为每个目标找到 alpha 网格所花费的迭代次数。
- n_features_in_int
在 拟合 期间看到的特征数。
0.24 版本新增。
- feature_names_in_shape 为 (
n_features_in_,) 的 ndarray 在 fit 期间看到的特征名称。仅当
X具有全部为字符串的特征名称时才定义。1.0 版本新增。
另请参阅
lars_pathCompute Least Angle Regression or Lasso path using LARS algorithm.
LarsCV交叉验证的最小角回归模型。
sklearn.decomposition.sparse_encode稀疏编码。
示例
>>> from sklearn import linear_model >>> reg = linear_model.Lars(n_nonzero_coefs=1) >>> reg.fit([[-1, 1], [0, 0], [1, 1]], [-1.1111, 0, -1.1111]) Lars(n_nonzero_coefs=1) >>> print(reg.coef_) [ 0. -1.11]
- fit(X, y, Xy=None)[source]#
Fit the model using X, y as training data.
- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
训练数据。
- yshape 为 (n_samples,) 或 (n_samples, n_targets) 的 array-like
目标值。
- Xyarray-like of shape (n_features,) or (n_features, n_targets), default=None
可以预计算的Xy = np.dot(X.T, y)。仅当Gram矩阵预计算时有用。
- 返回:
- selfobject
Returns an instance of self.
- get_metadata_routing()[source]#
获取此对象的元数据路由。
请查阅 用户指南,了解路由机制如何工作。
- 返回:
- routingMetadataRequest
封装路由信息的
MetadataRequest。
- 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]#
返回测试数据的 决定系数。
决定系数 \(R^2\) 定义为 \((1 - \frac{u}{v})\),其中 \(u\) 是残差平方和
((y_true - y_pred)** 2).sum(),\(v\) 是总平方和((y_true - y_true.mean()) ** 2).sum()。最佳得分是 1.0,它也可以是负数(因为模型可能任意地差)。一个总是预测y期望值而不考虑输入特征的常数模型将获得 0.0 的 \(R^2\) 分数。- 参数:
- 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\)。
注意事项
在回归器上调用
score时使用的 \(R^2\) 分数从版本 0.23 开始使用multioutput='uniform_average',以与r2_score的默认值保持一致。这会影响所有多输出回归器(除了MultiOutputRegressor)的score方法。
- set_fit_request(*, Xy: bool | None | str = '$UNCHANGED$') Lars[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
Xyparameter infit.
- 返回:
- selfobject
更新后的对象。
- set_params(**params)[source]#
设置此估计器的参数。
此方法适用于简单的估计器以及嵌套对象(如
Pipeline)。后者具有<component>__<parameter>形式的参数,以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计器参数。
- 返回:
- selfestimator instance
估计器实例。
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') Lars[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
更新后的对象。