Show Sidebar Hide Sidebar

Lasso and Elastic Net for Sparse Signals in Scikit-learn

Estimates Lasso and Elastic-Net regression models on a manually generated sparse signal corrupted with an additive noise. Estimated coefficients are compared with the ground-truth.

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

This tutorial imports r2_score, Lasso and ElasticNet.

In [2]:
print(__doc__)
 
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np
from sklearn.metrics import r2_score
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
Automatically created module for IPython interactive environment

Calculations

Generate some sparse data to play with

In [3]:
np.random.seed(42)

n_samples, n_features = 50, 200
X = np.random.randn(n_samples, n_features)
coef = 3 * np.random.randn(n_features)
inds = np.arange(n_features)
np.random.shuffle(inds)
coef[inds[10:]] = 0  # sparsify coef
y = np.dot(X, coef)

# add noise
y += 0.01 * np.random.normal((n_samples,))

# Split data in train set and test set
n_samples = X.shape[0]
X_train, y_train = X[:n_samples / 2], y[:n_samples / 2]
X_test, y_test = X[n_samples / 2:], y[n_samples / 2:]

Lasso

In [4]:
alpha = 0.1
lasso = Lasso(alpha=alpha)

y_pred_lasso = lasso.fit(X_train, y_train).predict(X_test)
r2_score_lasso = r2_score(y_test, y_pred_lasso)
print(lasso)
print("r^2 on test data : %f" % r2_score_lasso)
Lasso(alpha=0.1, copy_X=True, fit_intercept=True, max_iter=1000,
   normalize=False, positive=False, precompute=False, random_state=None,
   selection='cyclic', tol=0.0001, warm_start=False)
r^2 on test data : 0.384710

ElasticNet

In [5]:
enet = ElasticNet(alpha=alpha, l1_ratio=0.7)

y_pred_enet = enet.fit(X_train, y_train).predict(X_test)
r2_score_enet = r2_score(y_test, y_pred_enet)
print(enet)
print("r^2 on test data : %f" % r2_score_enet)
ElasticNet(alpha=0.1, copy_X=True, fit_intercept=True, l1_ratio=0.7,
      max_iter=1000, normalize=False, positive=False, precompute=False,
      random_state=None, selection='cyclic', tol=0.0001, warm_start=False)
r^2 on test data : 0.240176

Plot Results

In [6]:
p1 = go.Scatter(y=enet.coef_, 
                mode='lines',
                line=dict(color='lightgreen', width=2),
                name='Elastic net coefficients')

p2 = go.Scatter(y=lasso.coef_, 
                mode='lines',
                line=dict(color='gold', width=2),
                name='Lasso coefficients')

p3 = go.Scatter(y=coef, 
                mode='lines',
                line=dict(color='navy', dash='dash', width=2),
                name='original coefficients')
layout = go.Layout(title = "Lasso R^2: %f, Elastic Net R^2: %f"
                           % (r2_score_lasso, r2_score_enet),
                   hovermode='closest')

fig = go.Figure(data=[p1, p2, p3], layout=layout)
In [7]:
py.iplot(fig)
Out[7]:
Still need help?
Contact Us

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