Lasso 在密集和稀疏数据上的应用#

我们展示了 linear_model.Lasso 对密集和稀疏数据提供相同的结果,并且在稀疏数据的情况下速度有所提高。

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

from time import time

from scipy import linalg, sparse

from sklearn.datasets import make_regression
from sklearn.linear_model import Lasso

比较两种 Lasso 在密集数据上的实现#

我们创建一个适用于 Lasso 的线性回归问题,即特征数量多于样本数量。然后,我们以密集(常规)和稀疏两种格式存储数据矩阵,并分别在每种格式上训练 Lasso。我们计算两者的运行时间,并通过计算它们学习到的系数之间差异的欧几里得范数来检查它们是否学习了相同的模型。由于数据是密集的,我们期望使用密集数据格式能获得更好的运行时间。

X, y = make_regression(n_samples=200, n_features=5000, random_state=0)
# create a copy of X in sparse format
X_sp = sparse.coo_matrix(X)

alpha = 1
sparse_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=1000)
dense_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=1000)

t0 = time()
sparse_lasso.fit(X_sp, y)
print(f"Sparse Lasso done in {(time() - t0):.3f}s")

t0 = time()
dense_lasso.fit(X, y)
print(f"Dense Lasso done in {(time() - t0):.3f}s")

# compare the regression coefficients
coeff_diff = linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_)
print(f"Distance between coefficients : {coeff_diff:.2e}")

#
Sparse Lasso done in 0.120s
Dense Lasso done in 0.037s
Distance between coefficients : 1.01e-13

比较两种 Lasso 在稀疏数据上的实现#

我们将上述问题通过将所有小值替换为 0 来使其稀疏化,并进行相同的比较。由于数据现在是稀疏的,我们期望使用稀疏数据格式的实现会更快。

# make a copy of the previous data
Xs = X.copy()
# make Xs sparse by replacing the values lower than 2.5 with 0s
Xs[Xs < 2.5] = 0.0
# create a copy of Xs in sparse format
Xs_sp = sparse.coo_matrix(Xs)
Xs_sp = Xs_sp.tocsc()

# compute the proportion of non-zero coefficient in the data matrix
print(f"Matrix density : {(Xs_sp.nnz / float(X.size) * 100):.3f}%")

alpha = 0.1
sparse_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=10000)
dense_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=10000)

t0 = time()
sparse_lasso.fit(Xs_sp, y)
print(f"Sparse Lasso done in {(time() - t0):.3f}s")

t0 = time()
dense_lasso.fit(Xs, y)
print(f"Dense Lasso done in  {(time() - t0):.3f}s")

# compare the regression coefficients
coeff_diff = linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_)
print(f"Distance between coefficients : {coeff_diff:.2e}")
Matrix density : 0.626%
Sparse Lasso done in 0.187s
Dense Lasso done in  0.876s
Distance between coefficients : 8.65e-12

脚本总运行时间: (0 分钟 1.288 秒)

相关示例

稀疏信号的 L1 模型

稀疏信号的 L1 模型

多任务 Lasso 的联合特征选择

多任务 Lasso 的联合特征选择

Lasso、Lasso-LARS 和 Elastic Net 路径

Lasso、Lasso-LARS 和 Elastic Net 路径

Lasso 模型选择:AIC-BIC / 交叉验证

Lasso 模型选择:AIC-BIC / 交叉验证

由 Sphinx-Gallery 生成的画廊