2010-08-31 20:43:45 +08:00
|
|
|
"""
|
2010-11-05 21:54:09 +08:00
|
|
|
=====================
|
|
|
|
|
Lasso path using LARS
|
|
|
|
|
=====================
|
2010-08-31 20:43:45 +08:00
|
|
|
|
2010-11-05 21:54:09 +08:00
|
|
|
Computes Lasso Path along the regularization parameter using the LARS
|
2013-06-27 21:09:16 +08:00
|
|
|
algorithm on the diabetes dataset. Each color represents a different
|
2012-05-24 21:41:40 +08:00
|
|
|
feature of the coefficient vector, and this is displayed as a function
|
|
|
|
|
of the regularization parameter.
|
2010-09-21 17:00:24 +08:00
|
|
|
|
2010-08-31 20:43:45 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Fabian Pedregosa <fabian.pedregosa@inria.fr>
|
2010-09-01 00:37:41 +08:00
|
|
|
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2010-08-31 20:43:45 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2010-08-31 20:43:45 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import linear_model
|
|
|
|
|
from sklearn import datasets
|
2010-08-31 20:43:45 +08:00
|
|
|
|
2019-08-25 11:17:01 +08:00
|
|
|
X, y = datasets.load_diabetes(return_X_y=True)
|
2010-11-22 22:10:21 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Computing regularization path using the LARS ...")
|
2017-10-07 23:04:08 +08:00
|
|
|
_, _, coefs = linear_model.lars_path(X, y, method="lasso", verbose=True)
|
2010-12-09 16:32:32 +08:00
|
|
|
|
2010-12-10 01:57:45 +08:00
|
|
|
xx = np.sum(np.abs(coefs.T), axis=1)
|
|
|
|
|
xx /= xx[-1]
|
2010-08-31 20:43:45 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
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()
|