Show Sidebar Hide Sidebar

SGD Penalties in Scikit-learn

Plot the contours of the three penalties.

All of the above are supported by sklearn.linear_model.stochastic_gradient.

New to Plotly?¶

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

from __future__ import division
import numpy as np

Calculations¶

In [3]:
def l1(xs):
return np.array([np.sqrt((1 - np.sqrt(x ** 2.0)) ** 2.0) for x in xs])

def l2(xs):
return np.array([np.sqrt(1.0 - x ** 2.0) for x in xs])

def el(xs, z):
return np.array([(2 - 2 * x - 2 * z + 4 * x * z -
(4 * z ** 2
- 8 * x * z ** 2
+ 8 * x ** 2 * z ** 2
- 16 * x ** 2 * z ** 3
+ 8 * x * z ** 3 + 4 * x ** 2 * z ** 4) ** (1. / 2)
- 2 * x * z ** 2) / (2 - 4 * z) for x in xs])
def cross(ext):
p1 = go.Scatter(x=[-ext, ext], y=[0, 0],
mode='lines', line=dict(color='black', width=1),
showlegend=False)
p2 = go.Scatter(x=[0, 0], y=[-ext, ext],
mode='lines', line=dict(color='black', width=1),
showlegend=False)
return [p1, p2]
In [4]:
xs = np.linspace(0, 1, 100)

alpha = 0.501  # 0.5 division throuh zero

data = cross(1.2)

l1_color = "cyan"
l2_color = "blue"
elastic_net_color = "orange"
lw = 2
In [5]:
p1 = go.Scatter(x=xs, y=l1(xs),
mode='lines',
line=dict(color=l1_color, width=lw),
name="L1")
data.append(p1)

p2 = go.Scatter(x=xs, y=-1.0 * l1(xs),
mode='lines',
line=dict(color=l1_color, width=lw),
showlegend=False)
data.append(p2)

p3 = go.Scatter(x=-1 * xs, y=l1(xs),
mode='lines',
line=dict(color=l1_color, width=lw),
showlegend=False)
data.append(p3)

p4 = go.Scatter(x=-1 * xs, y=-1.0 * l1(xs),
mode='lines',
line=dict(color=l1_color, width=lw),
showlegend=False)
data.append(p4)

p5 = go.Scatter(x=xs, y=l2(xs),
mode='lines',
line=dict(color=l2_color, width=lw),
name='L2')
data.append(p5)

p6 = go.Scatter(x=xs, y=-1.0 * l2(xs),
mode='lines',
line=dict(color=l2_color, width=lw),
showlegend=False)
data.append(p6)

p7 = go.Scatter(x=-1 * xs, y=l2(xs),
mode='lines',
line=dict(color=l2_color, width=lw),
showlegend=False)
data.append(p7)

p8 = go.Scatter(x=-1 * xs, y=-1.0 * l2(xs),
mode='lines',
line=dict(color=l2_color, width=lw),
showlegend=False)
data.append(p8)

p9 = go.Scatter(x=xs, y=el(xs, alpha),
mode='lines',
line=dict(color=elastic_net_color, width=lw),
name="Elastic Net")
data.append(p9)

p10 = go.Scatter(x=xs, y=-1.0 * el(xs, alpha),
mode='lines',
line=dict(color=elastic_net_color, width=lw),
showlegend=False)
data.append(p10)

p11 = go.Scatter(x=-1 * xs, y=el(xs, alpha),
mode='lines',
line=dict(color=elastic_net_color, width=lw),
showlegend=False)
data.append(p11)

p12 = go.Scatter(x=-1 * xs, y=-1.0 * el(xs, alpha),
mode='lines',
line=dict(color=elastic_net_color, width=lw),
showlegend=False)
data.append(p12)

layout = go.Layout(xaxis=dict(title='w<sub>0</sub>',
zeroline=False, showgrid=False),
yaxis=dict(title='w<sub>1</sub>',
zeroline=False, showgrid=False),
hovermode='closest'
)
fig = go.Figure(data=data, layout=layout)
Still need help?