Skip to content
Snippets Groups Projects
Commit 6e982be9 authored by Alexandre Gramfort's avatar Alexandre Gramfort
Browse files

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

parent 87644dbc
No related branches found
No related tags found
No related merge requests found
...@@ -33,15 +33,17 @@ X[:,6] *= -1 # To reproduce wikipedia LAR page ...@@ -33,15 +33,17 @@ X[:,6] *= -1 # To reproduce wikipedia LAR page
print "Computing regularization path using the LARS ..." print "Computing regularization path using the LARS ..."
start = datetime.now() 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 print "This took ", datetime.now() - start
############################################################################### ###############################################################################
# Display path # 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() ymin, ymax = pl.ylim()
pl.vlines(-np.log10(alphas_), ymin, ymax, linestyle='dashed') pl.vlines(xx, ymin, ymax, linestyle='dashed')
pl.xlabel('-Log(lambda)') # XXX : wrong label pl.xlabel('|coef| / max|coef|')
pl.ylabel('Coefficients') pl.ylabel('Coefficients')
pl.title('Least Angle Regression (LAR) Path') pl.title('Least Angle Regression (LAR) Path')
pl.axis('tight') pl.axis('tight')
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
Lasso with Least Angle Regression Lasso with Least Angle Regression
================================= =================================
Computes Lasso Path with the LARS algorithm
""" """
print __doc__ print __doc__
...@@ -12,7 +14,6 @@ print __doc__ ...@@ -12,7 +14,6 @@ print __doc__
# License: BSD Style. # License: BSD Style.
from datetime import datetime from datetime import datetime
import itertools
import numpy as np import numpy as np
import pylab as pl import pylab as pl
...@@ -22,30 +23,22 @@ from scikits.learn import datasets ...@@ -22,30 +23,22 @@ from scikits.learn import datasets
diabetes = datasets.load_diabetes() diabetes = datasets.load_diabetes()
X = diabetes.data X = diabetes.data
y = diabetes.target y = diabetes.target
# someting's wrong with our dataset X[:,6] *= -1 # To reproduce wikipedia LASSO page
X[:, 6] = -X[:, 6]
################################################################################ ################################################################################
# Demo path functions # Demo path functions
G = np.dot(X.T, X)
print "Computing regularization path using the LARS ..." print "Computing regularization path using the LARS ..."
start = datetime.now() 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 print "This took ", datetime.now() - start
alphas = np.sum(np.abs(path.T), axis=1) xx = np.sum(np.abs(coefs_.T), axis=1)
alphas /= alphas[-1] xx /= xx[-1]
pl.plot(xx, coefs_.T)
# # Display results
color_iter = itertools.cycle(['r', 'g', 'b', 'c'])
for coef_, color in zip(path, color_iter):
pl.plot(alphas, coef_.T, color)
ymin, ymax = pl.ylim() ymin, ymax = pl.ylim()
pl.vlines(alphas, ymin, ymax, linestyle='dashed') pl.vlines(xx, ymin, ymax, linestyle='dashed')
pl.xlabel('-Log(lambda)') # XXX : wrong label pl.xlabel('|coef| / max|coef|')
pl.ylabel('Coefficients') pl.ylabel('Coefficients')
pl.title('LASSO Path') pl.title('LASSO Path')
pl.axis('tight') pl.axis('tight')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment