2010-09-14 21:00:01 +08:00
|
|
|
"""
|
|
|
|
|
==============================
|
|
|
|
|
Lasso on dense and sparse data
|
|
|
|
|
==============================
|
|
|
|
|
|
2012-09-02 01:09:34 +08:00
|
|
|
We show that linear_model.Lasso provides the same results for dense and sparse
|
|
|
|
|
data and that in the case of sparse data the speed is improved.
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
from time import time
|
|
|
|
|
from scipy import sparse
|
|
|
|
|
from scipy import linalg
|
|
|
|
|
|
2012-03-19 03:53:00 +08:00
|
|
|
from sklearn.datasets.samples_generator import make_regression
|
2012-09-02 01:09:34 +08:00
|
|
|
from sklearn.linear_model import Lasso
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-09-14 21:16:52 +08:00
|
|
|
# The two Lasso implementations on Dense data
|
2013-02-01 22:04:03 +08:00
|
|
|
print("--- Dense matrices")
|
2010-09-14 21:00:01 +08:00
|
|
|
|
2012-03-19 03:53:00 +08:00
|
|
|
X, y = make_regression(n_samples=200, n_features=5000, random_state=0)
|
2012-09-02 01:09:34 +08:00
|
|
|
X_sp = sparse.coo_matrix(X)
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
alpha = 1
|
2012-09-02 01:09:34 +08:00
|
|
|
sparse_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=1000)
|
|
|
|
|
dense_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=1000)
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
t0 = time()
|
2012-09-02 01:09:34 +08:00
|
|
|
sparse_lasso.fit(X_sp, y)
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Sparse Lasso done in %fs" % (time() - t0))
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
t0 = time()
|
2012-02-02 06:00:43 +08:00
|
|
|
dense_lasso.fit(X, y)
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Dense Lasso done in %fs" % (time() - t0))
|
2010-09-14 21:00:01 +08:00
|
|
|
|
2012-12-25 20:16:05 +08:00
|
|
|
print("Distance between coefficients : %s"
|
|
|
|
|
% linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_))
|
2010-09-14 21:00:01 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-09-14 21:16:52 +08:00
|
|
|
# The two Lasso implementations on Sparse data
|
2013-02-01 22:04:03 +08:00
|
|
|
print("--- Sparse matrices")
|
2010-09-14 21:00:01 +08:00
|
|
|
|
2010-09-14 21:16:52 +08:00
|
|
|
Xs = X.copy()
|
|
|
|
|
Xs[Xs < 2.5] = 0.0
|
|
|
|
|
Xs = sparse.coo_matrix(Xs)
|
2010-09-14 21:00:01 +08:00
|
|
|
Xs = Xs.tocsc()
|
|
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Matrix density : %s %%" % (Xs.nnz / float(X.size) * 100))
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
alpha = 0.1
|
2012-09-02 01:09:34 +08:00
|
|
|
sparse_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=10000)
|
|
|
|
|
dense_lasso = Lasso(alpha=alpha, fit_intercept=False, max_iter=10000)
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
t0 = time()
|
2012-02-02 06:00:43 +08:00
|
|
|
sparse_lasso.fit(Xs, y)
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Sparse Lasso done in %fs" % (time() - t0))
|
2010-09-14 21:00:01 +08:00
|
|
|
|
|
|
|
|
t0 = time()
|
2014-05-21 09:16:34 +08:00
|
|
|
dense_lasso.fit(Xs.toarray(), y)
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Dense Lasso done in %fs" % (time() - t0))
|
2010-09-14 21:00:01 +08:00
|
|
|
|
2012-12-25 20:16:05 +08:00
|
|
|
print("Distance between coefficients : %s"
|
|
|
|
|
% linalg.norm(sparse_lasso.coef_ - dense_lasso.coef_))
|