econml.sklearn_extensions.linear_model.WeightedLasso

class econml.sklearn_extensions.linear_model.WeightedLasso(alpha=1.0, fit_intercept=True, precompute=False, copy_X=True, max_iter=1000, tol=0.0001, warm_start=False, positive=False, random_state=None, selection='cyclic')[source]

Bases: econml.sklearn_extensions.linear_model.WeightedModelMixin, sklearn.linear_model._coordinate_descent.Lasso

接受权重的 sklearn Lasso 版本。

参数
  • alpha (float, optional) – 乘以 L1 项的常数。默认为 1.0。alpha = 0 等效于普通最小二乘法,由 LinearRegression 对象求解。出于数值原因,不建议将 alpha = 0 与 Lasso 一起使用。考虑到这一点,你应该使用 LinearRegression 对象。

  • 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 的解决方案作为初始化,否则,仅清除之前的解决方案。请参阅 术语表

  • positive (bool, optional) – 设置为 True 时,强制系数为正。

  • 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 时。

coef_

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

类型

数组,形状 (n_features,) | (n_targets, n_features)

intercept_

决策函数中的独立项。

类型

浮点数 | 数组,形状 (n_targets,)

n_iter_

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

类型

整数 | 类数组对象,形状 (n_targets,)

__init__(alpha=1.0, fit_intercept=True, precompute=False, copy_X=True, max_iter=1000, tol=0.0001, warm_start=False, positive=False, random_state=None, selection='cyclic')[source]

方法

__init__([alpha, fit_intercept, precompute, ...])

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

使用坐标下降拟合模型。

get_params([deep])

获取此估计器的参数。

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

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

predict(X)

使用线性模型进行预测。

score(X, y[, sample_weight])

返回预测的决定系数。

set_params(**params)

设置此估计器的参数。

属性

sparse_coef_

拟合的 coef_ 的稀疏表示。

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

使用坐标下降拟合模型。

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

  • y (ndarray, shape (n_samples,) or (n_samples, n_targets)) – 目标。如果需要,将转换为 X 的数据类型

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

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

get_params(deep=True)

获取此估计器的参数。

参数

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

返回

params (dict) – 参数名称及其对应值。

返回类型

dict

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 混合范数作为正则化器训练的多任务弹性网络模型。

MultiTaskElasticNetCV

带有内置交叉验证的多任务 L1/L2 弹性网络。

ElasticNet

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

ElasticNetCV

沿正则化路径进行迭代拟合的弹性网络模型。

注意事项

有关示例,请参阅 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,)) – 返回预测值。

返回类型

array, 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 的期望值的常数模型,忽略输入特征,将获得 \(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\)self.predict(X) 关于 y 的值。

返回类型

浮点数

注意事项

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

set_params(**params)

设置此估计器的参数。

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

参数

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

返回

self (estimator instance) – 估计器实例。

返回类型

估计器实例

property sparse_coef_

拟合的 coef_ 的稀疏表示。