scikit-learn/examples/linear_model/plot_lar.py

52 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2010-06-16 15:45:55 +08:00
"""
2010-09-20 20:58:32 +08:00
============================
Least Angle Regression (LAR)
============================
Compute LAR path on diabetes dataset.
See: http://en.wikipedia.org/wiki/Least-angle_regression
2010-06-16 15:45:55 +08:00
"""
2010-09-20 20:58:32 +08:00
print __doc__
2010-06-16 15:45:55 +08:00
# Author: Fabian Pedregosa <fabian.pedregosa@inria.fr>
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
2010-06-16 15:45:55 +08:00
# License: BSD Style.
from datetime import datetime
import numpy as np
import pylab as pl
from scikits.learn import linear_model
2010-06-16 16:57:59 +08:00
from scikits.learn import datasets
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
2010-06-16 16:57:59 +08:00
2010-09-20 20:58:32 +08:00
X[:,6] *= -1 # To reproduce wikipedia LAR page
2010-06-16 15:45:55 +08:00
################################################################################
2010-09-20 20:58:32 +08:00
# Compute path functions
2010-06-16 15:45:55 +08:00
print "Computing regularization path using the LARS ..."
start = datetime.now()
_, _, coefs_ = linear_model.lars_path(X, y, max_features=10, method="lasso")
2010-06-16 15:45:55 +08:00
print "This took ", datetime.now() - start
2010-09-20 20:58:32 +08:00
###############################################################################
# Display path
xx = np.sum(np.abs(coefs_), axis=0)
xx /= xx[-1]
pl.plot(xx, coefs_.T)
2010-06-17 20:34:31 +08:00
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')
2010-06-16 15:45:55 +08:00
pl.axis('tight')
pl.show()