lars_path#

sklearn.linear_model.lars_path(X, y, Xy=None, *, Gram=None, max_iter=500, alpha_min=0, method='lar', copy_X=True, eps=np.float64(2.220446049250313e-16), copy_Gram=True, verbose=0, return_path=True, return_n_iter=False, positive=False)[source]#

使用 LARS 算法计算最小角回归或 Lasso 路径。

当 method=’lasso’ 时,优化目标为

(1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1

当 method=’lar’ 时,目标函数仅以隐式方程的形式已知(参见 [1] 中的讨论)。

Read more in the User Guide.

参数:
XNone or ndarray of shape (n_samples, n_features)

输入数据。如果 X 是 None,则 Gram 也必须是 None。如果只有 Gram 矩阵可用,请改用 lars_path_gram

yNone or ndarray of shape (n_samples,)

输入目标值。

Xyarray-like of shape (n_features,), default=None

Xy = X.T @ y 可以预先计算。仅当 Gram 矩阵预先计算时才有用。

GramNone, ‘auto’, bool, ndarray of shape (n_features, n_features), default=None

预先计算的 Gram 矩阵 X.T @ X。如果设置为 'auto',则当样本数多于特征数时,会从给定的 X 预先计算 Gram 矩阵。

max_iterint, default=500

要执行的最大迭代次数,设置为无穷大表示没有限制。

alpha_minfloat, default=0

路径上的最小相关性。它对应于 Lasso 中的正则化参数 alpha

method{‘lar’, ‘lasso’}, default=’lar’

指定返回的模型。选择 'lar' 代表最小角回归(Least Angle Regression),'lasso' 代表 Lasso。

copy_Xbool, default=True

如果为 False,则会覆盖 X

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_Grambool, default=True

如果为 False,则会覆盖 Gram

verboseint, default=0

控制输出详细程度。

return_pathbool, default=True

如果为 True,返回整个路径;否则只返回路径的最后一点。

return_n_iterbool, default=False

是否返回迭代次数。

positivebool, default=False

限制系数 >= 0。此选项仅在 method ‘lasso’ 时允许使用。请注意,对于较小的 alpha 值,模型系数不会收敛到普通最小二乘解。只有达到分步 Lars-Lasso 算法所达到的最小 alpha 值(当 fit_path=True 时为 alphas_[alphas_ > 0.].min())的系数才通常与坐标下降 lasso_path 函数的解一致。

返回:
alphasndarray of shape (n_alphas + 1,)

每次迭代中协方差的最大值(绝对值)。n_alphasmax_itern_features,或者路径中 alpha >= alpha_min 的节点数,取其中较小者。

activendarray of shape (n_alphas,)

路径结束时活动变量的索引。

coefsndarray of shape (n_features, n_alphas + 1)

路径上的系数。

n_iter整型

运行的迭代次数。仅当 return_n_iter 设置为 True 时返回。

另请参阅

lars_path_gram

在充分统计模式下计算 LARS 路径。

lasso_path

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

LassoLars

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

Lars

最小角回归模型,又名 LAR。

LassoLarsCV

使用 LARS 算法的交叉验证 Lasso。

LarsCV

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

sklearn.decomposition.sparse_encode

稀疏编码。

References

示例

>>> from sklearn.linear_model import lars_path
>>> from sklearn.datasets import make_regression
>>> X, y, true_coef = make_regression(
...    n_samples=100, n_features=5, n_informative=2, coef=True, random_state=0
... )
>>> true_coef
array([ 0.        ,  0.        ,  0.        , 97.9, 45.7])
>>> alphas, _, estimated_coef = lars_path(X, y)
>>> alphas.shape
(3,)
>>> estimated_coef
array([[ 0.     ,  0.     ,  0.     ],
       [ 0.     ,  0.     ,  0.     ],
       [ 0.     ,  0.     ,  0.     ],
       [ 0.     , 46.96, 97.99],
       [ 0.     ,  0.     , 45.70]])