Skip to content

Commit

Permalink
using diabetes in lasso/lars examples
Browse files Browse the repository at this point in the history
  • Loading branch information
agramfort committed Sep 20, 2010
1 parent 78fbd20 commit 69ca71e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 45 deletions.
32 changes: 15 additions & 17 deletions examples/glm/plot_lar.py
@@ -1,17 +1,21 @@
#!/usr/bin/env python
"""
======================
Least Angle Regression
======================
============================
Least Angle Regression (LAR)
============================
Compute LAR path on diabetes dataset.
See: http://en.wikipedia.org/wiki/Least-angle_regression
"""
print __doc__

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

from datetime import datetime
import itertools
import numpy as np
import pylab as pl

Expand All @@ -22,27 +26,21 @@
X = diabetes.data
y = diabetes.target

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

################################################################################
# Demo path functions
################################################################################
# Compute path functions

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

alphas = -np.log10(clf.alphas_)

# # Display results
color_iter = itertools.cycle(['r', 'g', 'b', 'c'])

for coef_, color in zip(clf.coef_path_, color_iter):
pl.plot(alphas, coef_.T, color)

###############################################################################
# Display path
pl.plot(-np.log10(alphas_), coefs_.T)
ymin, ymax = pl.ylim()
pl.vlines(alphas, ymin, ymax, linestyle='dashed')
pl.vlines(-np.log10(alphas_), ymin, ymax, linestyle='dashed')
pl.xlabel('-Log(lambda)') # XXX : wrong label
pl.ylabel('Coefficients')
pl.title('Least Angle Regression (LAR) Path')
Expand Down
24 changes: 12 additions & 12 deletions examples/glm/plot_lasso_coordinate_descent_path.py
Expand Up @@ -6,6 +6,7 @@
Lasso and elastic net (L1 and L2 penalisation) implemented using a
coordinate descent.
"""
print __doc__

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

from scikits.learn.glm import lasso_path, enet_path
from scikits.learn import datasets

n_samples, n_features = 100, 10
np.random.seed(0)
y = np.random.randn(n_samples)
X = np.random.randn(n_samples, n_features)
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target

################################################################################
# Fit models
################################################################################
X /= X.std(0) # Standardize data (easier to set the rho parameter)

################################################################################
# Demo path functions
################################################################################
# Compute paths

eps = 1e-2 # the smaller it is the longer is the path
eps = 5e-3 # the smaller it is the longer is the path

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

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

################################################################################
# Display results

color_iter = cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
for color, coef_lasso, coef_enet in zip(color_iter,
coefs_lasso.T, coefs_enet.T):
pl.plot(-np.log10(alphas_lasso), coef_lasso, color)
pl.plot(-np.log10(alphas_enet), coef_enet, color + 'x')
pl.plot(-np.log10(alphas_enet), coef_enet, color + '--')

pl.xlabel('-Log(lambda)')
pl.ylabel('weights')
Expand Down
17 changes: 1 addition & 16 deletions examples/glm/plot_lasso_lars.py
Expand Up @@ -5,6 +5,7 @@
=================================
"""
print __doc__

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


m, n = 200, 200
np.random.seed(0)
X = np.random.randn(m, n)
y = np.random.randn(m)


_xmean = X.mean(0)
_ymean = y.mean(0)
X = X - _xmean
y = y - _ymean
_norms = np.apply_along_axis (np.linalg.norm, 0, X)
nonzeros = np.flatnonzero(_norms)
X[:, nonzeros] /= _norms[nonzeros]

################################################################################
# Demo path functions
################################################################################

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

0 comments on commit 69ca71e

Please sign in to comment.