econml.sklearn_extensions.linear_model.DebiasedLasso

class econml.sklearn_extensions.linear_model.DebiasedLasso(alpha='auto', n_alphas=100, alpha_cov='auto', n_alphas_cov=10, fit_intercept=True, precompute=False, copy_X=True, max_iter=1000, tol=0.0001, warm_start=False, random_state=None, selection='cyclic', n_jobs=None)[source]

基类: econml.sklearn_extensions.linear_model.WeightedLasso

去偏 Lasso 模型。

实现源自 https://arxiv.org/abs/1303.0518

仅针对单维度输出实现。

参数
  • alpha (str | float, default ‘auto’.) – 乘以 L1 项的常数。默认为 ‘auto’。 alpha = 0 等同于普通最小二乘法,由 LinearRegression 对象求解。出于数值原因,不建议将 alpha = 0Lasso 对象一起使用。因此,您应该使用 LinearRegression 对象。

  • n_alphas (int, default 100) – 如果 alpha=’auto’,要尝试的 alpha 数量

  • alpha_cov (str | float, default ‘auto’) – 用于构建协方差矩阵 Theta 的伪逆的正则化 alpha,用于校正 lasso 系数。每一次这样的回归对应于一个特征对剩余特征的回归。

  • n_alphas_cov (int, default 10) – 如果 alpha_cov=’auto’,要尝试的 alpha_cov 数量。

  • fit_intercept (bool, default True) – 是否为该模型计算截距。如果设置为 False,计算中将不使用截距(例如,假定数据已居中)。

  • precompute (True | False | array_like, default False) – 是否使用预先计算的 Gram 矩阵来加速计算。如果设置为 'auto',则由我们决定。Gram 矩阵也可以作为参数传递。对于稀疏输入,此选项始终为 True 以保留稀疏性。

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

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

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

  • warm_start (bool, optional) – 设置为 True 时,重用之前调用 fit 的解决方案作为初始化,否则,仅擦除之前的解决方案。参见 词汇表

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

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

  • n_jobs (int, optional) – 调用并行计算时要使用的作业数量

coef_

参数向量(成本函数公式中的 w)。

类型

array, shape (n_features,)

intercept_

决策函数中的独立项。

类型

float

n_iter_

坐标下降求解器为达到指定容差而运行的迭代次数。

类型

int | array_like, shape (n_targets,)

selected_alpha_

通过交叉验证选择的惩罚项,如果 alpha=’auto’。

类型

float

coef_stderr_

系数的估计标准误差(参见 coef_ 属性)。

类型

array, shape (n_features,)

intercept_stderr_

截距的估计标准误差(参见 intercept_ 属性)。

类型

float

__init__(alpha='auto', n_alphas=100, alpha_cov='auto', n_alphas_cov=10, fit_intercept=True, precompute=False, copy_X=True, max_iter=1000, tol=0.0001, warm_start=False, random_state=None, selection='cyclic', n_jobs=None)[source]

方法

__init__([alpha, n_alphas, alpha_cov, ...])

coef__interval([alpha])

获取限定拟合系数的置信区间。

fit(X, y[, sample_weight, check_input])

拟合去偏 lasso 模型。

get_params([deep])

获取此估计器的参数。

intercept__interval([alpha])

获取限定拟合截距的置信区间。

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

使用坐标下降计算弹性网络路径。

predict(X)

使用线性模型进行预测。

predict_interval(X[, alpha])

使用去偏 lasso 构建预测置信区间。

prediction_stderr(X)

使用去偏 lasso 获取预测的标准误差。

score(X, y[, sample_weight])

返回预测的决定系数。

set_params(**params)

设置此估计器的参数。

属性

sparse_coef_

拟合 coef_ 的稀疏表示。

coef__interval(alpha=0.05)[source]

获取限定拟合系数的置信区间。

参数

alpha (float, default 0.05) – 置信水平。将计算参数分布的 alpha/2 分位数和 (1-alpha/2) 分位数作为置信区间

返回

(coef_lower, coef_upper) – 返回系数的区间下限和上限。

返回类型

tuple of array, shape (n_coefs, )

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

拟合去偏 lasso 模型。

参数
  • X (ndarray or scipy.sparse matrix, (n_samples, n_features)) – 输入数据。

  • y (array, shape (n_samples,)) – 目标变量。如有必要,将转换为 X 的 dtype

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

  • check_input (bool, default True) – 允许绕过一些输入检查。除非您知道自己在做什么,否则不要使用此参数。

get_params(deep=True)

获取此估计器的参数。

参数

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

返回

params – 参数名称映射到其值。

返回类型

dict

intercept__interval(alpha=0.05)[source]

获取限定拟合截距的置信区间。

参数

alpha (float, default 0.05) – 置信水平。将计算参数分布的 alpha/2 分位数和 (1-alpha/2) 分位数作为置信区间

返回

(intercept_lower, intercept_upper) – 返回截距的区间下限和上限。

返回类型

tuple of floats

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)

使用坐标下降计算弹性网络路径。

弹性网络优化函数对于单输出和多输出有所不同。

对于单输出任务,它是

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}

即每行范数的总和。

用户指南中了解更多信息。

参数
  • 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)) – 目标值。

  • 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 矩阵来加速计算。如果设置为 '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 时允许)。

  • check_input (bool, default=True) – 如果设置为 False,则跳过输入验证检查(包括提供 Gram 矩阵时)。假定它们由调用者处理。

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

返回

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

  • 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 达到指定容差所花费的迭代次数。(当 return_n_iter 设置为 True 时返回)。

另请参阅

MultiTaskElasticNet

使用 L1/L2 混合范数作为正则化器训练的多任务 ElasticNet 模型。

MultiTaskElasticNetCV

内置交叉验证的多任务 L1/L2 ElasticNet。

ElasticNet

结合 L1 和 L2 先验作为正则化器的线性回归。

ElasticNetCV

沿正则化路径进行迭代拟合的 Elastic Net 模型。

注意

示例请参见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,)

predict_interval(X, alpha=0.05)[source]

使用去偏 lasso 构建预测置信区间。

参数
  • X (ndarray or scipy.sparse matrix, (n_samples, n_features)) – 样本。

  • alpha (float in [0, 1], default 0.05) – 所报告区间的总体置信水平。报告的是 alpha/2, 1-alpha/2 置信区间。

返回

(y_lower, y_upper) – 返回区间下限和上限。

返回类型

tuple of array, shape (n_samples, )

prediction_stderr(X)[source]

使用去偏 lasso 获取预测的标准误差。

参数

X (ndarray or scipy.sparse matrix, (n_samples, n_features)) – 样本。

返回

prediction_stderr – 我们预测的每个点处输出的每个坐标的标准误差

返回类型

array_like, shape (n_samples, )

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 的期望值的常数模型将获得 0.0 的 \(R^2\) 分数。

参数
  • 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) – 样本权重。

返回

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

返回类型

float

注意

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

set_params(**params)

设置此估计器的参数。

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

参数

**params (dict) – 估计器参数。

返回

self – 估计器实例。

返回类型

估计器实例

property sparse_coef_

拟合 coef_ 的稀疏表示。