Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scikit-learn
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ian Johnson
scikit-learn
Commits
545f47c7
Commit
545f47c7
authored
14 years ago
by
Alexandre Gramfort
Browse files
Options
Downloads
Patches
Plain Diff
FIX : example of dense vs sparse Lasso on dense and sparse data
parent
bb2e0663
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/glm/lasso_dense_vs_sparse_data.py
+11
-13
11 additions, 13 deletions
examples/glm/lasso_dense_vs_sparse_data.py
with
11 additions
and
13 deletions
examples/glm/lasso_dense_vs_sparse_data.py
+
11
−
13
View file @
545f47c7
...
...
@@ -4,9 +4,8 @@ Lasso on dense and sparse data
==============================
We show that glm.Lasso and glm.sparse.Lasso
provide the same results.
XXX : At the end of the day it should also lead to a speed improvement
provide the same results and that in the case of
sparse data glm.sparse.Lasso improves the speed.
"""
...
...
@@ -20,10 +19,10 @@ from scikits.learn.glm import Lasso as DenseLasso
###############################################################################
# The two Lasso implementation on Dense data
# The two Lasso implementation
s
on Dense data
print
"
--- Dense matrices
"
n_samples
,
n_features
=
1
00
,
10000
n_samples
,
n_features
=
2
00
,
10000
np
.
random
.
seed
(
0
)
y
=
np
.
random
.
randn
(
n_samples
)
X
=
np
.
random
.
randn
(
n_samples
,
n_features
)
...
...
@@ -44,27 +43,26 @@ print "Distance between coefficients : %s" % linalg.norm(sparse_lasso.coef_
-
dense_lasso
.
coef_
)
###############################################################################
# The two Lasso implementation on Sparse data
# The two Lasso implementation
s
on Sparse data
print
"
--- Sparse matrices
"
Xs
=
sparse
.
coo_matrix
(
X
)
mask
=
Xs
.
data
>
2
# Sparsify data matrix
col
=
Xs
.
col
[
mask
]
row
=
Xs
.
row
[
mask
]
Xs
=
X
.
copy
()
Xs
[
Xs
<
2.5
]
=
0.0
Xs
=
sparse
.
coo_matrix
(
Xs
)
Xs
=
Xs
.
tocsc
()
print
"
Matrix density : %s %%
"
%
(
mask
.
sum
()
/
float
(
X
.
size
)
*
100
)
print
"
Matrix density : %s %%
"
%
(
Xs
.
nnz
/
float
(
X
.
size
)
*
100
)
alpha
=
0.1
sparse_lasso
=
SparseLasso
(
alpha
=
alpha
,
fit_intercept
=
False
)
dense_lasso
=
DenseLasso
(
alpha
=
alpha
,
fit_intercept
=
False
)
t0
=
time
()
sparse_lasso
.
fit
(
Xs
,
y
,
maxit
=
1000
,
tol
=
0.0
)
sparse_lasso
.
fit
(
Xs
,
y
,
maxit
=
1000
)
print
"
Sparse Lasso done in %fs
"
%
(
time
()
-
t0
)
t0
=
time
()
dense_lasso
.
fit
(
Xs
.
todense
(),
y
,
maxit
=
1000
,
tol
=
0.0
)
dense_lasso
.
fit
(
Xs
.
todense
(),
y
,
maxit
=
1000
)
print
"
Dense Lasso done in %fs
"
%
(
time
()
-
t0
)
print
"
Distance between coefficients : %s
"
%
linalg
.
norm
(
sparse_lasso
.
coef_
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment