scikit-learn/examples/linear_model/plot_lasso_path_crossval.py

75 lines
2.0 KiB
Python
Raw Normal View History

"""
==================================================
Cross validated Lasso path with coordinate descent
==================================================
Compute a 20-fold cross-validated Lasso path with coordinate descent to
find the optimal value of alpha.
Note how the optimal value of alpha varies for each fold. This
illustrates why nested-cross validation is necessary when trying to
evaluate the performance of a method for which a parameter is chosen by
cross-validation: this choice of parameter may not be optimal for unseen
data.
"""
print __doc__
# Author: Olivier Grisel
# License: BSD Style.
import numpy as np
import pylab as pl
from scikits.learn.linear_model import LassoCV
from scikits.learn.cross_val import KFold
from scikits.learn import datasets
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
2011-07-26 09:41:48 +08:00
# normalize data as done by Lars to allow for comparison
X /= np.sqrt(np.sum(X ** 2, axis=0))
2010-12-12 08:13:58 +08:00
##############################################################################
# Compute paths
eps = 1e-3 # the smaller it is the longer is the path
print "Computing regularization path using the lasso..."
model = LassoCV(eps=eps, cv=KFold(len(y), 20)).fit(X, y)
2010-12-12 08:13:58 +08:00
##############################################################################
# Display results
m_log_alphas = -np.log10(model.alphas)
m_log_alpha = -np.log10(model.alpha)
ax = pl.gca()
ax.set_color_cycle(2 * ['b', 'r', 'g', 'c', 'k'])
pl.subplot(2, 1, 1)
pl.plot(m_log_alphas, model.coef_path_)
ymin, ymax = pl.ylim()
pl.vlines([m_log_alpha], ymin, ymax, linestyle='dashed')
pl.xticks(())
pl.ylabel('weights')
pl.title('Lasso paths')
pl.axis('tight')
pl.subplot(2, 1, 2)
ymin, ymax = 2300, 3800
pl.plot(m_log_alphas, model.mse_path_, '--')
pl.plot(m_log_alphas, model.mse_path_.mean(axis=-1), 'k',
label='Average accross the folds')
pl.vlines([m_log_alpha], ymin, ymax, linestyle='dashed')
pl.legend(loc='best')
pl.xlabel('-log(lambda)')
pl.ylabel('MSE')
pl.title('Mean Square Errors on each CV fold')
pl.axis('tight')
pl.ylim(ymin, ymax)
pl.show()