econml.sklearn_extensions.linear_model.WeightedMultiTaskLassoCV

class econml.sklearn_extensions.linear_model.WeightedMultiTaskLassoCV(eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, max_iter=1000, tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=None, random_state=None, selection='cyclic')[source]

支持权重的 sklearn MultiTaskLassoCV 版本。

参数

eps (float, optional) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3
  • n_alphas (int, optional) – 正则化路径上的 alphas 数量

  • alphas (array_like, optional) – 计算模型的 alphas 列表。如果未提供,则自动设置。

  • fit_intercept (bool) – 是否计算此模型的截距。如果设置为 false,计算中将不使用截距(例如,数据已预期中心化)。

  • max_iter (int, optional) – 最大迭代次数。

  • tol (float, optional) – 优化的容差:如果更新小于 tol,则优化代码检查对偶间隙以获得最优性,并继续直到它小于 tol

  • copy_X (bool, default True) – 如果为 True,将复制 X;否则,可能会被覆盖。

  • cv (int, cross-validation generator or an iterable, optional) – 确定交叉验证分割策略。cv 的可能输入包括: - None,使用默认的 3 折加权交叉验证, - 整数,指定折数。 - CV 分割器, - 生成 (train, test) 分割作为索引数组的可迭代对象。对于整数/None 输入,使用 WeightedKFold

  • 如果为 None,则使用 5 折。

    verbose (bool or int) – 详细程度。

  • n_jobs (int or None, optional) – 交叉验证期间使用的 CPU 数量。请注意,仅当给定多个 l1_ratio 值时才使用此参数。 None 表示 1,除非在 joblib.parallel_backend 上下文中。 -1 表示使用所有处理器。有关详细信息,请参阅 术语表

  • random_state (int, RandomState instance, or None, default None) – 伪随机数生成器的种子,用于选择要更新的随机特征。如果为 int,则 random_state 是随机数生成器使用的种子;如果为 RandomState 实例,则 random_state 是随机数生成器;如果为 None,则随机数生成器是 RandomState 实例,由 np.random 使用。在 selection='random' 时使用。

  • selection (str, default ‘cyclic’) – 如果设置为 ‘random’,则每次迭代更新一个随机系数,而不是默认情况下按顺序循环遍历特征。这(设置为 ‘random’)通常会导致收敛速度显著加快,尤其当 tol 高于 1e-4 时。

  • __init__(eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, max_iter=1000, tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=None, random_state=None, selection='cyclic')[source]

方法

__init__([eps, n_alphas, alphas, ...])

fit(X, y[, sample_weight])

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

get_params([deep])

获取此估计器的参数。

path(X, y, *[, eps, n_alphas, alphas, ...])

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

predict(X)

使用线性模型进行预测。

score(X, y[, sample_weight])

返回预测的决定系数。

set_params(**params)

设置此估计器的参数。

fit(X, y, sample_weight=None)[source]

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

get_params([deep])

eps (float, optional) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3
  • X (ndarray or scipy.sparse matrix, (n_samples, n_features)) – 数据

  • y (ndarray, shape (n_samples,) or (n_samples, n_targets)) – 目标值。如有必要,将被转换为 X 的 dtype

  • sample_weight (numpy array of shape [n_samples]) – 每个样本的个体权重。权重将在内部归一化。

get_params(deep=True)

path(X, y, *[, eps, n_alphas, alphas, ...])

eps (float, optional) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3

获取此估计器的参数。

deep (bool, default=True) – 如果为 True,将返回此估计器以及包含的作为估计器的子对象的参数。

返回

params – 参数名称与其值的映射。

返回类型

dict

predict(X)

static path(X, y, *, 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, **params)

Lasso 优化函数对于单输出和多输出任务有所不同。

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

对于单输出任务,它是

(1 / (2 * n_samples)) * ||Y - XW||^2_Fro + alpha * ||W||_21

对于多输出任务,它是

||W||_21 = \sum_i \sqrt{\sum_j w_{ij}^2}

其中

即每行范数的总和。

eps (float, optional) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3
  • 用户指南 中了解更多。

  • X ({array-like, sparse matrix} of shape (n_samples, n_features)) – 训练数据。直接以 Fortran 连续数据形式传递可避免不必要的内存复制。如果 y 是单输出,则 X 可以是稀疏的。

  • y ({array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_targets)) – 目标值。

  • eps (float, default=1e-3) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3

  • n_alphas (int, default=100) – 正则化路径上的 alphas 数量。

  • alphas (ndarray, default=None) – 计算模型的 alphas 列表。如果为 None,则自动设置 alphas。

  • precompute (‘auto’, bool or array-like of shape (n_features, n_features), default=’auto’) – 是否使用预计算的 Gram 矩阵来加速计算。如果设置为 'auto',则由我们决定。也可以将 Gram 矩阵作为参数传递。

  • Xy (array-like of shape (n_features,) or (n_features, n_targets), default=None) – 可预计算的 Xy = np.dot(X.T, y)。仅当 Gram 矩阵已预计算时有用。

  • copy_X (bool, default=True) – 如果为 True,将复制 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 时允许)。

deep (bool, default=True) – 如果为 True,将返回此估计器以及包含的作为估计器的子对象的参数。

  • **params (kwargs) – 传递给坐标下降求解器的关键字参数。

  • alphas (ndarray of shape (n_alphas,)) – 计算模型路径上的 alphas。

  • coefs (ndarray of shape (n_features, n_alphas) or (n_targets, n_features, n_alphas)) – 路径上的系数。

  • dual_gaps (ndarray of shape (n_alphas,)) – 每个 alpha 的优化结束时的对偶间隙。

n_iters (list of int) – 坐标下降优化器为每个 alpha 达到指定容差所需的迭代次数。

另请参阅

lars_path

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

Lasso

Lasso 是一个估计稀疏系数的线性模型。

LassoLars

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

LassoCV

沿正则化路径进行迭代拟合的 Lasso 线性模型。

LassoLarsCV

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

sklearn.decomposition.sparse_encode

一种估计器,可用于将信号转换为固定原子集合的稀疏线性组合。

注意

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

为了避免不必要的内存复制,fit 方法的 X 参数应直接作为 Fortran 连续的 numpy 数组传递。

请注意,在某些情况下,Lars 求解器在实现此功能方面可能显著更快。特别是,可以使用线性插值来检索 lars_path 输出值之间的模型系数

示例

>>> import numpy as np
>>> from sklearn.linear_model import lasso_path
>>> X = np.array([[1, 2, 3.1], [2.3, 5.4, 4.3]]).T
>>> y = np.array([1, 2, 3.1])
>>> # Use lasso_path to compute a coefficient path
>>> _, coef_path, _ = lasso_path(X, y, alphas=[5., 1., .5])
>>> print(coef_path)
[[0.         0.         0.46874778]
 [0.2159048  0.4425765  0.23689075]]
>>> # Now use lars_path and 1D linear interpolation to compute the
>>> # same path
>>> from sklearn.linear_model import lars_path
>>> alphas, active, coef_path_lars = lars_path(X, y, method='lasso')
>>> from scipy import interpolate
>>> coef_path_continuous = interpolate.interp1d(alphas[::-1],
...                                             coef_path_lars[:, ::-1])
>>> print(coef_path_continuous([5., 1., .5]))
[[0.         0.         0.46915237]
 [0.2159048  0.4425765  0.23668876]]
比较 lasso_path 和 lars_path 以及插值

score(X, y[, sample_weight])

eps (float, optional) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3

predict(X)

deep (bool, default=True) – 如果为 True,将返回此估计器以及包含的作为估计器的子对象的参数。

使用线性模型进行预测。

params – 参数名称与其值的映射。

X (array-like or sparse matrix, shape (n_samples, n_features)) – 样本。

C – 返回预测值。

set_params(**params)

array, shape (n_samples,)

eps (float, optional) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3
  • score(X, y, sample_weight=None)

  • 决定系数 \(R^2\) 定义为 \((1 - \frac{u}{v})\),其中 \(u\) 是残差平方和 ((y_true - y_pred)** 2).sum()\(v\) 是总平方和 ((y_true - y_true.mean()) ** 2).sum()。最佳可能得分为 1.0,也可能为负(因为模型可能任意差)。一个总是预测 y 期望值而忽略输入特征的常数模型将得到 \(R^2\) 分数为 0.0。

  • 返回预测的决定系数。

deep (bool, default=True) – 如果为 True,将返回此估计器以及包含的作为估计器的子对象的参数。

X (array-like of shape (n_samples, n_features)) – 测试样本。对于某些估计器,这可能是预计算的核矩阵或泛型对象列表,其形状为 (n_samples, n_samples_fitted),其中 n_samples_fitted 是估计器拟合中使用的样本数量。

params – 参数名称与其值的映射。

y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – X 的真实值。

一种估计器,可用于将信号转换为固定原子集合的稀疏线性组合。

sample_weight (array-like of shape (n_samples,), default=None) – 样本权重。

scoreself.predict(X) 相对于 y\(R^2\)

fit(X, y, sample_weight=None)[source]

float

eps (float, optional) – 路径长度。 eps=1e-3 意味着 alpha_min / alpha_max = 1e-3

在回归器上调用 score 时使用的 \(R^2\) 分数从 0.23 版本开始使用 multioutput='uniform_average',以与 r2_score() 的默认值保持一致。这影响了所有多输出回归器(MultiOutputRegressor 除外)的 score 方法。

deep (bool, default=True) – 如果为 True,将返回此估计器以及包含的作为估计器的子对象的参数。

set_params(**params)

params – 参数名称与其值的映射。

设置此估计器的参数。