Skip to content

Commit 69ca71e

Browse files
committed
using diabetes in lasso/lars examples
1 parent 78fbd20 commit 69ca71e

File tree

3 files changed

+28
-45
lines changed

3 files changed

+28
-45
lines changed

examples/glm/plot_lar.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#!/usr/bin/env python
22
"""
3-
======================
4-
Least Angle Regression
5-
======================
3+
============================
4+
Least Angle Regression (LAR)
5+
============================
6+
7+
Compute LAR path on diabetes dataset.
8+
9+
See: http://en.wikipedia.org/wiki/Least-angle_regression
610
711
"""
12+
print __doc__
813

914
# Author: Fabian Pedregosa <fabian.pedregosa@inria.fr>
1015
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
1116
# License: BSD Style.
1217

1318
from datetime import datetime
14-
import itertools
1519
import numpy as np
1620
import pylab as pl
1721

@@ -22,27 +26,21 @@
2226
X = diabetes.data
2327
y = diabetes.target
2428

29+
X[:,6] *= -1 # To reproduce wikipedia LAR page
2530

2631
################################################################################
27-
# Demo path functions
28-
################################################################################
32+
# Compute path functions
2933

3034
print "Computing regularization path using the LARS ..."
3135
start = datetime.now()
32-
# should not use a fit predict to get the path
33-
clf = glm.LeastAngleRegression().fit(X, y, normalize=True)
36+
alphas_, _, coefs_ = glm.lars_path(X, y, max_iter=9, method="lar")
3437
print "This took ", datetime.now() - start
3538

36-
alphas = -np.log10(clf.alphas_)
37-
38-
# # Display results
39-
color_iter = itertools.cycle(['r', 'g', 'b', 'c'])
40-
41-
for coef_, color in zip(clf.coef_path_, color_iter):
42-
pl.plot(alphas, coef_.T, color)
43-
39+
###############################################################################
40+
# Display path
41+
pl.plot(-np.log10(alphas_), coefs_.T)
4442
ymin, ymax = pl.ylim()
45-
pl.vlines(alphas, ymin, ymax, linestyle='dashed')
43+
pl.vlines(-np.log10(alphas_), ymin, ymax, linestyle='dashed')
4644
pl.xlabel('-Log(lambda)') # XXX : wrong label
4745
pl.ylabel('Coefficients')
4846
pl.title('Least Angle Regression (LAR) Path')

examples/glm/plot_lasso_coordinate_descent_path.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Lasso and elastic net (L1 and L2 penalisation) implemented using a
77
coordinate descent.
88
"""
9+
print __doc__
910

1011
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
1112
# License: BSD Style.
@@ -15,38 +16,37 @@
1516
import pylab as pl
1617

1718
from scikits.learn.glm import lasso_path, enet_path
19+
from scikits.learn import datasets
1820

19-
n_samples, n_features = 100, 10
20-
np.random.seed(0)
21-
y = np.random.randn(n_samples)
22-
X = np.random.randn(n_samples, n_features)
21+
diabetes = datasets.load_diabetes()
22+
X = diabetes.data
23+
y = diabetes.target
2324

24-
################################################################################
25-
# Fit models
26-
################################################################################
25+
X /= X.std(0) # Standardize data (easier to set the rho parameter)
2726

2827
################################################################################
29-
# Demo path functions
30-
################################################################################
28+
# Compute paths
3129

32-
eps = 1e-2 # the smaller it is the longer is the path
30+
eps = 5e-3 # the smaller it is the longer is the path
3331

3432
print "Computing regularization path using the lasso..."
3533
models = lasso_path(X, y, eps=eps)
3634
alphas_lasso = np.array([model.alpha for model in models])
3735
coefs_lasso = np.array([model.coef_ for model in models])
3836

3937
print "Computing regularization path using the elastic net..."
40-
models = enet_path(X, y, eps=eps, rho=0.6)
38+
models = enet_path(X, y, eps=eps, rho=0.8)
4139
alphas_enet = np.array([model.alpha for model in models])
4240
coefs_enet = np.array([model.coef_ for model in models])
4341

42+
################################################################################
4443
# Display results
44+
4545
color_iter = cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
4646
for color, coef_lasso, coef_enet in zip(color_iter,
4747
coefs_lasso.T, coefs_enet.T):
4848
pl.plot(-np.log10(alphas_lasso), coef_lasso, color)
49-
pl.plot(-np.log10(alphas_enet), coef_enet, color + 'x')
49+
pl.plot(-np.log10(alphas_enet), coef_enet, color + '--')
5050

5151
pl.xlabel('-Log(lambda)')
5252
pl.ylabel('weights')

examples/glm/plot_lasso_lars.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
=================================
66
77
"""
8+
print __doc__
89

910
# Author: Fabian Pedregosa <fabian.pedregosa@inria.fr>
1011
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
@@ -24,24 +25,8 @@
2425
# someting's wrong with our dataset
2526
X[:, 6] = -X[:, 6]
2627

27-
28-
m, n = 200, 200
29-
np.random.seed(0)
30-
X = np.random.randn(m, n)
31-
y = np.random.randn(m)
32-
33-
34-
_xmean = X.mean(0)
35-
_ymean = y.mean(0)
36-
X = X - _xmean
37-
y = y - _ymean
38-
_norms = np.apply_along_axis (np.linalg.norm, 0, X)
39-
nonzeros = np.flatnonzero(_norms)
40-
X[:, nonzeros] /= _norms[nonzeros]
41-
4228
################################################################################
4329
# Demo path functions
44-
################################################################################
4530

4631
G = np.dot(X.T, X)
4732
print "Computing regularization path using the LARS ..."

0 commit comments

Comments
 (0)