sklearn.linear_model.MultiTaskElasticNetCV

class sklearn.linear_model.MultiTaskElasticNetCV(*, l1_ratio=0.5, eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, max_iter=1000, tol=0.0001, cv=None, copy_X=True, verbose=0, n_jobs=None, random_state=None, selection='cyclic')

[源码]

具有内置交叉验证的多任务L1 或 L2 弹性网。

有关交叉验证估算器,请参阅词汇表条目。

多任务弹性网(MultiTaskElasticNet)的优化目标是:

这里

即每一行的范数之和。

用户指南中阅读更多内容。

0.15版的新功能。

参数 说明
l1_ratio float or list of float, default=0.5
Elastic-Net(弹性网)混合参数,其中0 <l1_ratio <=1。对于l1_ratio = 1,惩罚是L1 / L2惩罚。对于l1_ratio = 0,这是L2惩罚。对于0 < l1_ratio <1,惩罚是L1/L2和L2的组合。此参数可以是列表,在这种情况下,通过交叉验证测试不同的值,并使用给出最佳预测得分的值。注意,l1_ratio值列表的一个很好的选择通常是使更多的值接近1(即Lasso),少接近0(即Ridge),如[.1, .5, .7, .9, .95, .99, 1]
eps float, default=1e-3
路径的长度。eps=1e-3意味着alpha_min / alpha_max = 1e-3
n_alphas int, default=100
沿正则化路径的Alpha个数。
alphas ndarray, default=None
用于计算模型的alpha列表。如果为None自动设置Alpha。
fit_intercept bool, default=True
是否计算该模型的截距。如果设置为false,则在计算中将不使用截距(即数据应中心化)。
normalize bool, default=False
fit_intercept设置为False 时,将忽略此参数。如果为True,则在回归之前通过减去均值并除以l2-范数来对回归变量X进行归一化。如果你希望标准化,请先使用 sklearn.preprocessing.StandardScaler,然后调用fit 估算器并设置normalize=False
max_iter int, default=1000
最大迭代次数。
tol float, default=1e-4
优化的容忍度:如果更新小于tol,则优化代码检查对偶间隙的最优性,并继续进行直到其小于tol
cv int, cross-validation generator or iterable, default=None
确定交叉验证拆分策略。可能的输入是:

- None,使用默认的5折交叉验证
- int,用于指定折数。
- CV分割器
- 可迭代生成(分割训练、测试)索引数组。

对于int / None输入,使用KFold

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

在0.22版本中更改:cv如果是“None”,默认值从3更改为5。
copy_X bool, default=True
如果True,将复制X;否则X可能会被覆盖。
verbose bool or int, default=0
日志详细程度。
n_jobs int, default=None
交叉验证期间要使用的CPU核心数量。 除非在上下文中设置了joblib.parallel_backend,否则None表示1 。 -1表示使用所有处理器。有关更多详细信息,请参见词汇表
random_state int, RandomState instance, default=None
更新特征随机选择的伪随机数生成器种子。在selection=='random'时使用。在多个函数调用之间传递同一个整数实现可重复的输出。请参阅词汇表
selection {‘cyclic’, ‘random’}, default=’cyclic’
如果设置为“random”,则随机系数将在每次迭代时更新,而不是默认情况下按顺序遍历特征。这(设置为“random”)通常会导致收敛更快,尤其是当tol高于1e-4时。
属性 说明
intercept_ ndarray of shape (n_tasks,)
目标函数中的截距。
coef_ ndarray of shape (n_tasks, n_features)
参数向量(目标函数公式中的w)。coef_存储W的转置W.T
alpha_ float
通过交叉验证选择的惩罚量。
mse_path_ ndarray of shape (n_alphas, n_folds) or (n_l1_ratio, n_alphas, n_folds)
每一折中不同的alpha对应测试集的均方误差。
alphas_ ndarray of shape (n_alphas,) or (n_l1_ratio, n_alphas)
对于每个l1_ratio,用于拟合的Alpha网格
l1_ratio_ float
通过交叉验证获得的最佳l1_ratio。
n_iter_ int
坐标下降求解器运行的迭代次数,以达到最佳alpha的指定容忍度。

另见

用坐标下降算法拟合模型。

为避免不必要的内存重复,fit方法的X和y参数应作为Fortran-contiguous numpy数组直接传递。

示例

>>> from sklearn import linear_model
>>> clf = linear_model.MultiTaskElasticNetCV(cv=3)
>>> clf.fit([[0,0], [11], [22]],
...         [[00], [11], [22]])
MultiTaskElasticNetCV(cv=3)
>>> print(clf.coef_)
[[0.52875032 0.46958558]
 [0.52875032 0.46958558]]
>>> print(clf.intercept_)
[0.00166409 0.00166409]

方法

方法 说明
fit(X, y) 用坐标下降法拟合线性模型
get_params([deep]) 获取此估计器的参数。
path(X, y, *[, l1_ratio, eps, n_alphas, …]) 计算具有坐标下降的弹性净路径。
predict(X) 使用线性模型进行预测。
score(X, y[, sample_weight]) 返回预测的确定系数R ^ 2。
set_params(**params) 设置此估算器的参数。
__init__(*, l1_ratio=0.5, eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, normalize=False, max_iter=1000, tol=0.0001, cv=None, copy_X=True, verbose=0, n_jobs=None, random_state=None, selection='cyclic')

[源码]

初始化self, 请参阅help(type(self))以获得准确的说明。

fit(X, y)

[源码]

用坐标下降法拟合线性模型。

在Alpha网格上拟合模型,并通过交叉验证估算出最佳Alpha。

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练数据。直接作为Fortran-contiguous数据传递,以避免不必要的内存重复。如果y是单输出,则X可以是稀疏的。
y array-like of shape (n_samples,) or (n_samples, n_targets)
目标值。
get_params(deep=True)

[源码]

获取此估计器的参数。

参数 说明
deep bool, default=True
如果为True,则将返回此估算器和所包含子对象的参数。
返回值 说明
params mapping of string to any
参数名称映射到其值。
static 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)

[源码]

计算具有坐标下降的弹性网路径。

弹性网优化函数针对单输出和多输出而变化。

对于单输出任务,它是:

对于多输出任务,它是:

这里

即每一行的范数之和。

用户指南中阅读更多内容。

参数 说明
X {array-like, sparse matrix} of shape (n_samples, n_features)
训练数据。直接作为Fortran-contiguous数据传递,以避免不必要的内存重复。如果y是单输出,则X 可以是稀疏的。
y {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_outputs)
目标值。
l1_ratio float, default=0.5
0到1之间的数字传递给弹性网(在L1和L2惩罚之间缩放)。l1_ratio=1对应于Lasso。
eps float, default=1e-3
路径的长度。eps=1e-3意味着 alpha_min / alpha_max = 1e-3
n_alphas int, default=100
沿着正则化路径的Alpha个数。
alphas ndarray, default=None
用于计算模型的alpha列表。如果为None,则自动设置Alpha。
precompute ‘auto’, bool or array-like of shape (n_features, n_features), default=’auto’
是否使用预先计算的Gram矩阵来加快计算速度。Gram矩阵也可以作为参数传递。
Xy array-like of shape (n_features,) or (n_features, n_outputs), default=None
Xy = np.dot(XT,y)可以预先计算。仅当预先计算了Gram矩阵时才有用。
copy_X bool, default=True
如果True,将复制X;否则X可能会被覆盖。
coef_init ndarray of shape (n_features, ), default=None
系数的初始值。
verbose bool or int, default=False
详细程度。
return_n_iter bool, default=False
是否返回迭代次数。
positive bool, default=False
如果设置为True,则强制系数为正。(仅当y.ndim == 1时允许)。
check_input bool, default=True
跳过输入验证检查,包括提供的Gram矩阵(假设提供),假设在check_input = False时由调用方处理。
**params kwargs
传递给坐标下降求解器的关键字参数。
返回值 说明
alphas ndarray of shape (n_alphas,)
沿模型计算路径的Alpha值。
coefs ndarray of shape (n_features, n_alphas) or (n_outputs, n_features, n_alphas)
沿路径的系数。
dual_gaps ndarray of shape (n_alphas,)
每个alpha优化结束时的双重间隔。
n_iters list of int
坐标下降优化器为达到每个alpha的指定容差所进行的迭代次数。(当return_n_iter设置为True 时返回)。

另见

有关示例,请参见examples/linear_model/plot_lasso_coordinate_descent_path.py.

predict(X)

[源码]

使用线性模型进行预测。

参数 说明
X array_like or sparse matrix, shape (n_samples, n_features)
样本数据
返回值 说明
C array, shape (n_samples,)
返回预测值。
score(X, y, sample_weight=None)

[源码]

返回预测的确定系数R ^ 2。

系数R ^ 2定义为(1- u / v),其中u是残差平方和((y_true-y_pred)** 2).sum(),而v是总平方和((y_true- y_true.mean())** 2).sum()。可能的最高得分为1.0,并且也可能为负(因为该模型可能会更差)。一个常数模型总是预测y的期望值,而不考虑输入特征,得到的R^2得分为0.0。

参数 说明
X array-like of shape (n_samples, n_features)
测试样本。对于某些估计器,这可以是预先计算的内核矩阵或通用对象列表,形状为(n_samples,n_samples_fitted),其中n_samples_fitted是用于拟合估计器的样本数。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
X的真实值。
sample_weight array-like of shape (n_samples,), default=None
样本权重。
返回值 说明
score float
预测值与真实值的R^2。

调用回归器中的score时使用的R2分数,multioutput='uniform_average'从0.23版开始使用 ,与r2_score默认值保持一致。这会影响多输出回归的score方法( MultiOutputRegressor除外)。

set_params(**params)

[源码]

设置此估计器的参数。

该方法适用于简单的估计器以及嵌套对象(例如管道)。后者具有形式为 <component>__<parameter>的参数,这样就可以更新嵌套对象的每个组件。

参数 说明
**params dict
估计器参数。
返回值 说明
self object
估计器实例。