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] 中的讨论)。
在用户指南中阅读更多内容。
- 参数:
- XNone 或 shape 为 (n_samples, n_features) 的 ndarray
输入数据。如果 X 为
None
,则 Gram 也必须为None
。如果仅提供了 Gram 矩阵,请改用lars_path_gram
。- yNone 或 shape 为 (n_samples,) 的 ndarray
输入目标。
- Xyshape 为 (n_features,) 的类数组对象,默认值为 None
Xy = X.T @ y
,可以预先计算。仅当 Gram 矩阵预先计算时才有用。- GramNone, ‘auto’, bool, shape 为 (n_features, n_features) 的 ndarray,默认值为 None
预先计算的 Gram 矩阵
X.T @ X
,如果为'auto'
,则当样本数量多于特征数量时,Gram 矩阵会从给定的 X 中预先计算。- max_iterint,默认值为 500
要执行的最大迭代次数,设置为无穷大表示无限制。
- alpha_minfloat,默认值为 0
路径上的最小相关性。它对应于 Lasso 中的正则化参数
alpha
。- method{‘lar’, ‘lasso’},默认值为 ‘lar’
指定返回的模型。选择
'lar'
用于最小角回归,'lasso'
用于 Lasso。- copy_Xbool,默认值为 True
如果为
False
,则X
会被覆盖。- epsfloat,默认值为 np.finfo(float).eps
Cholesky 对角因子计算中的机器精度正则化。对于病态程度很高的系统,请增加此值。与某些基于迭代优化的算法中的
tol
参数不同,此参数不控制优化的容差。- copy_Grambool,默认值为 True
如果为
False
,则Gram
会被覆盖。- verboseint,默认值为 0
控制输出详细程度。
- return_pathbool,默认值为 True
如果为
True
,则返回完整路径;否则,仅返回路径的最后一个点。- return_n_iterbool,默认值为 False
是否返回迭代次数。
- positivebool,默认值为 False
将系数限制为 >= 0。此选项仅在方法为 ‘lasso’ 时允许。请注意,对于较小的 alpha 值,模型系数将不会收敛到普通最小二乘解。只有通过逐步 Lars-Lasso 算法达到的最小 alpha 值(当 fit_path=True 时为
alphas_[alphas_ > 0.].min()
)对应的系数,通常才与坐标下降lasso_path
函数的解一致。
- 返回:
- alphasshape 为 (n_alphas + 1,) 的 ndarray
每次迭代时协方差的最大值(绝对值)。
n_alphas
是max_iter
、n_features
或路径中alpha >= alpha_min
的节点数中的较小者。- activeshape 为 (n_alphas,) 的 ndarray
路径结束时活跃变量的索引。
- coefsshape 为 (n_features, n_alphas + 1) 的 ndarray
沿路径的系数。
- n_iterint
运行的迭代次数。仅当
return_n_iter
设置为 True 时返回。
另请参阅
lars_path_gram
在充分统计模式下计算 LARS 路径。
lasso_path
使用坐标下降法计算 Lasso 路径。
LassoLars
使用最小角回归(即 Lars)拟合的 Lasso 模型。
Lars
最小角回归模型(即 LAR)。
LassoLarsCV
交叉验证的 Lasso,使用 LARS 算法。
LarsCV
交叉验证的最小角回归模型。
sklearn.decomposition.sparse_encode
稀疏编码。
参考文献
示例
>>> 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]])