Show Sidebar Hide Sidebar

Lasso Path Using LARS in Scikit-learn

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.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version

In [1]:
import sklearn
sklearn.__version__
Out[1]:
'0.18.1'

Imports

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np
from sklearn import linear_model
from sklearn import datasets

Calculations

In [3]:
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]
Computing regularization path using the LARS ...
.

Plot Results

In [4]:
data = [ ]
for i in range(0, len(coefs)):
    trace = go.Scatter(x=xx, y=coefs[i],
                       mode='lines', showlegend=False)
    data.append(trace)

for i in range(0, len(xx)):
    trace1 = go.Scatter(x=2* [xx[i]], y=[-800, 800],
                        mode='lines', showlegend=False,
                        line=dict(color='black', width=1,
                                  dash='dash')
                       )
    data.append(trace1)
    
layout = go.Layout(title='LASSO Path',
                   xaxis=dict(title='|coef| / max|coef|'),
                   yaxis=dict(title='Coefficients'))
fig = go.Figure(data=data, layout=layout)
In [5]:
py.iplot(fig)
Out[5]:

License

Author:

     Fabian Pedregosa <fabian.pedregosa@inria.fr>

     Alexandre Gramfort <alexandre.gramfort@inria.fr>

License:

     BSD 3 clause
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.