Lasso path using LARS

Computes Lasso Path along the regularization parameter using the LARS algorithm on the diabetes dataset. Each color represents a different feature of the coefficient vector, and this is displayed as a function of the regularization parameter.

../../_images/sphx_glr_plot_lasso_lars_001.png

Out:

1
2
  Computing regularization path using the LARS ...
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
print(__doc__)
 
# Author: Fabian Pedregosa <fabian.pedregosa@inria.fr>
#         Alexandre Gramfort <alexandre.gramfort@inria.fr>
# License: BSD 3 clause
 
import numpy as np
import matplotlib.pyplot as plt
 
from sklearn import linear_model
from sklearn import datasets
 
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
 
print("Computing regularization path using the LARS ...")
alphas, _, coefs = linear_model.lars_path(X, y, method='lasso', verbose=True)
 
xx = np.sum(np.abs(coefs.T), axis=1)
xx /= xx[-1]
 
plt.plot(xx, coefs.T)
ymin, ymax = plt.ylim()
plt.vlines(xx, ymin, ymax, linestyle='dashed')
plt.xlabel('|coef| / max|coef|')
plt.ylabel('Coefficients')
plt.title('LASSO Path')
plt.axis('tight')
plt.show()

Total running time of the script: (0 minutes 0.107 seconds)

Download Python source code: plot_lasso_lars.py
Download IPython notebook: plot_lasso_lars.ipynb
doc_scikit_learn
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.