# Author: Fabian Pedregosa <*****@*****.**>
#         Alexandre Gramfort <*****@*****.**>
# License: BSD Style.

import numpy as np
import pylab as pl

from scikits.learn import glm
from scikits.learn import datasets

diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target

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

print "Computing regularization path using the LARS ..."
_, _, coefs_ = glm.lars_path(X, y, method='lasso', verbose=True)

xx = np.sum(np.abs(coefs_.T), axis=1)
xx /= xx[-1]
pl.plot(xx, coefs_.T)
ymin, ymax = pl.ylim()
pl.vlines(xx, ymin, ymax, linestyle='dashed')
pl.xlabel('|coef| / max|coef|')
pl.ylabel('Coefficients')
pl.title('LASSO Path')
pl.axis('tight')
pl.show()

_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 ..."
start = datetime.now()
alphas, active, path = glm.lars_path(X, y, Gram=G, 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)

ymin, ymax = pl.ylim()
pl.vlines(alphas, ymin, ymax, linestyle='dashed')
pl.xlabel('-Log(lambda)') # XXX : wrong label
pl.ylabel('Coefficients')
Exemple #3
0
from scikits.learn import glm
from scikits.learn import datasets

diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target

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

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

print "Computing regularization path using the LARS ..."
start = datetime.now()
_, _, coefs_ = glm.lars_path(X, y, max_features=10, method="lar")
print "This took ", datetime.now() - start

###############################################################################
# Display path
xx = np.sum(np.abs(coefs_), axis=0)
xx /= xx[-1]
pl.plot(xx, coefs_.T)
ymin, ymax = pl.ylim()
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')
pl.show()
import numpy as np
import pylab as pl

from scikits.learn import glm
from scikits.learn import datasets

diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
X[:,6] *= -1 # To reproduce wikipedia LASSO page

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

print "Computing regularization path using the LARS ..."
start = datetime.now()
alphas_, _, coefs_ = glm.lars_path(X, y, method='lasso')
print "This took ", datetime.now() - start

xx = np.sum(np.abs(coefs_.T), axis=1)
xx /= xx[-1]
pl.plot(xx, coefs_.T)
ymin, ymax = pl.ylim()
pl.vlines(xx, ymin, ymax, linestyle='dashed')
pl.xlabel('|coef| / max|coef|')
pl.ylabel('Coefficients')
pl.title('LASSO Path')
pl.axis('tight')
pl.show()