econml.sklearn_extensions.linear_model.WeightedLassoCV

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

接受权重的 sklearn LassoCV 版本。

参数

eps (float, optional) – 路径的长度。eps=1e-3 表示 alpha_min / alpha_max = 1e-3
  • n_alphas (int, optional) – 沿着正则化路径的 alpha 数量

  • alphas (numpy array, optional) – 计算模型的 alpha 列表。如果为 None,则自动设置 alpha 值

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

  • precompute (True | False | ‘auto’ | array_like) – 是否使用预先计算的 Gram 矩阵来加速计算。如果设置为 'auto',则由程序决定。Gram 矩阵也可以作为参数传入。

  • 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 splitter, - 可迭代对象,生成 (train, test) 分割作为索引数组。 对于整数/None 输入,使用 WeightedKFold

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

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

  • n_jobs (int or None, optional) – 交叉验证期间使用的 CPU 数量。None 表示 1,除非在 joblib.parallel_backend 上下文中。-1 表示使用所有处理器。详情请参阅 术语表

  • positive (bool, optional) – 如果为正,则限制回归系数为正

  • 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, precompute='auto', max_iter=1000, tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=None, positive=False, 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]

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

get_params([deep])

eps (float, optional) – 路径的长度。eps=1e-3 表示 alpha_min / alpha_max = 1e-3
  • y (ndarray, shape (n_samples,) or (n_samples, n_targets)) – 目标。必要时将转换为 X 的 dtype。

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

  • get_params(deep=True)

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

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

eps (float, optional) – 路径的长度。eps=1e-3 表示 alpha_min / alpha_max = 1e-3

返回值

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

返回类型

dict

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 优化函数对于单输出和多输出任务不同。

predict(X)

对于单输出任务,它是

对于多输出任务,它是

(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}

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

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

eps (float, optional) – 路径的长度。eps=1e-3 表示 alpha_min / alpha_max = 1e-3
  • 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) – 沿正则化路径的 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 时允许)。

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

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

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

  • 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 算法计算 Least Angle Regression 或 Lasso 路径。

Lasso

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

LassoLars

使用 Least Angle Regression(又称 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 输出值之间的模型系数。

示例

比较带有插值的 lasso_path 和 lars_path

predict(X)

>>> 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]]
X (array-like or sparse matrix, shape (n_samples, n_features)) – 样本。

score(X, y[, sample_weight])

eps (float, optional) – 路径的长度。eps=1e-3 表示 alpha_min / alpha_max = 1e-3

C – 返回预测值。

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

array, shape (n_samples,)

dict

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。

set_params(**params)

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

eps (float, optional) – 路径的长度。eps=1e-3 表示 alpha_min / alpha_max = 1e-3
  • 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\)

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

float

dict

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

示例请参阅 examples/linear_model/plot_lasso_coordinate_descent_path.py

set_params(**params)

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

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

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

eps (float, optional) – 路径的长度。eps=1e-3 表示 alpha_min / alpha_max = 1e-3

self – 估计器实例。

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

估计器实例

dict