Permalink
Browse files

pretifying the LAR / LARS examples to match with results on wikipedia…

… page
  • Loading branch information...
1 parent 87644db commit 6e982be900b9c26f28d984b70ff3ed7b05129189 @agramfort agramfort committed Sep 21, 2010
Showing with 15 additions and 20 deletions.
  1. +6 −4 examples/glm/plot_lar.py
  2. +9 −16 examples/glm/plot_lasso_lars.py
View
10 examples/glm/plot_lar.py
@@ -33,15 +33,17 @@
print "Computing regularization path using the LARS ..."
start = datetime.now()
-alphas_, _, coefs_ = glm.lars_path(X, y, max_features=9, method="lar")
+_, _, coefs_ = glm.lars_path(X, y, max_features=10, method="lar")
print "This took ", datetime.now() - start
###############################################################################
# Display path
-pl.plot(-np.log10(alphas_), coefs_.T)
+xx = np.sum(np.abs(coefs_), axis=0)
+xx /= xx[-1]
+pl.plot(xx, coefs_.T)
ymin, ymax = pl.ylim()
-pl.vlines(-np.log10(alphas_), ymin, ymax, linestyle='dashed')
-pl.xlabel('-Log(lambda)') # XXX : wrong label
+pl.vlines(xx, ymin, ymax, linestyle='dashed')
+pl.xlabel('|coef| / max|coef|')
pl.ylabel('Coefficients')
pl.title('Least Angle Regression (LAR) Path')
pl.axis('tight')
View
25 examples/glm/plot_lasso_lars.py
@@ -4,6 +4,8 @@
Lasso with Least Angle Regression
=================================
+Computes Lasso Path with the LARS algorithm
+
"""
print __doc__
@@ -12,7 +14,6 @@
# License: BSD Style.
from datetime import datetime
-import itertools
import numpy as np
import pylab as pl
@@ -22,30 +23,22 @@
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
-# someting's wrong with our dataset
-X[:, 6] = -X[:, 6]
+X[:,6] *= -1 # To reproduce wikipedia LASSO page
################################################################################
# Demo path functions
-G = np.dot(X.T, X)
print "Computing regularization path using the LARS ..."
start = datetime.now()
-alphas, active, path = glm.lars_path(X, y, Gram=G, method='lasso')
+alphas_, _, coefs_ = glm.lars_path(X, y, method='lasso')
print "This took ", datetime.now() - start
-alphas = np.sum(np.abs(path.T), axis=1)
-alphas /= alphas[-1]
-
-# # Display results
-color_iter = itertools.cycle(['r', 'g', 'b', 'c'])
-
-for coef_, color in zip(path, color_iter):
- pl.plot(alphas, coef_.T, color)
-
+xx = np.sum(np.abs(coefs_.T), axis=1)
+xx /= xx[-1]
+pl.plot(xx, coefs_.T)
ymin, ymax = pl.ylim()
-pl.vlines(alphas, ymin, ymax, linestyle='dashed')
-pl.xlabel('-Log(lambda)') # XXX : wrong label
+pl.vlines(xx, ymin, ymax, linestyle='dashed')
+pl.xlabel('|coef| / max|coef|')
pl.ylabel('Coefficients')
pl.title('LASSO Path')
pl.axis('tight')

0 comments on commit 6e982be

Please sign in to comment.