enet_path#
- sklearn.linear_model.enet_path(X, y, *, l1_ratio=0.5, eps=0.001, n_alphas=100, alphas=None, precompute='auto', Xy=None, copy_X=True, coef_init=None, verbose=False, return_n_iter=False, positive=False, check_input=True, **params)[source]#
使用坐标下降计算 elastic net 路径。
弹性网络优化函数对于单输出和多输出任务有所不同。
对于单输出任务,它是
1 / (2 * n_samples) * ||y - Xw||^2_2 + alpha * l1_ratio * ||w||_1 + 0.5 * alpha * (1 - l1_ratio) * ||w||^2_2
对于多输出任务,它是
(1 / (2 * n_samples)) * ||Y - XW||_Fro^2 + alpha * l1_ratio * ||W||_21 + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2
其中
||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2}
即每行范数之和。
在用户指南中阅读更多内容。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练数据。直接作为Fortran连续数据传递以避免不必要的内存复制。如果
y是单输出,则X可以是稀疏的。- y{array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_targets)
目标值。
- l1_ratiofloat, default=0.5
传递给弹性网络的值介于0和1之间(L1和L2惩罚之间的缩放)。
l1_ratio=1对应于Lasso。- epsfloat, default=1e-3
路径的长度。
eps=1e-3表示alpha_min / alpha_max = 1e-3。- n_alphasint, default=100
正则化路径上alpha的数量。
- alphasarray-like, default=None
计算模型的alpha列表。如果为None,则自动设置alphas。
- precompute‘auto’, bool or array-like of shape (n_features, n_features), default=’auto’
是否使用预计算的Gram矩阵来加快计算速度。如果设置为
'auto',则由我们决定。Gram矩阵也可以作为参数传入。- Xyarray-like of shape (n_features,) or (n_features, n_targets), default=None
可以预计算的Xy = np.dot(X.T, y)。仅当Gram矩阵预计算时有用。
- copy_Xbool, default=True
如果为
True,X将被复制;否则,它可能会被覆盖。- coef_initarray-like of shape (n_features, ), default=None
系数的初始值。
- verbosebool or int, default=False
冗余度级别。
- return_n_iterbool, default=False
是否返回迭代次数。
- positivebool, default=False
如果设置为True,强制系数为正。(仅当
y.ndim == 1时允许)。- check_inputbool, default=True
如果设置为False,则跳过输入验证检查(包括提供的Gram矩阵)。假设这些检查由调用者处理。
- **paramskwargs
传递给坐标下降求解器的关键字参数。
- 返回:
- alphasndarray of shape (n_alphas,)
计算模型的路径上的alphas。
- coefsndarray of shape (n_features, n_alphas) or (n_targets, n_features, n_alphas)
路径上的系数。
- dual_gapsndarray of shape (n_alphas,)
每个alpha优化结束时的对偶间隙。
- n_iterslist of int
坐标下降优化器为达到每个alpha的指定容忍度所花费的迭代次数。(当
return_n_iter设置为True时返回)。
另请参阅
MultiTaskElasticNet使用 L1/L2 混合范数作为正则化项训练的多任务 ElasticNet 模型。
MultiTaskElasticNetCV具有内置交叉验证的多任务 L1/L2 ElasticNet。
ElasticNet具有组合 L1 和 L2 先验作为正则化项的线性回归。
ElasticNetCV具有沿正则化路径迭代拟合的 Elastic Net 模型。
注意事项
有关示例,请参阅examples/linear_model/plot_lasso_lasso_lars_elasticnet_path.py。
底层的坐标下降求解器使用间隙安全筛选规则来加快拟合时间,请参阅坐标下降用户指南。
示例
>>> from sklearn.linear_model import enet_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, _ = enet_path(X, y, n_alphas=3) >>> alphas.shape (3,) >>> estimated_coef array([[ 0., 0.787, 0.568], [ 0., 1.120, 0.620], [-0., -2.129, -1.128], [ 0., 23.046, 88.939], [ 0., 10.637, 41.566]])