Show Sidebar Hide Sidebar

# Isotonic Regression in Scikit-learn

How to plot Isotonic Regression in Python with Plotly.

An illustration of the isotonic regression on generated data. The isotonic regression finds a non-decreasing approximation of a function while minimizing the mean squared error on the training data. The benefit of such a model is that it does not assume any form for the target function such as linearity. For comparison a linear regression is also presented.

#### 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!

### Imports¶

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

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.isotonic import IsotonicRegression
from sklearn.utils import check_random_state


### Calculations¶

In [3]:
print(__doc__)

n = 100
x = np.arange(n)
rs = check_random_state(0)
y = rs.randint(-50, 50, size=(n,)) + 50. * np.log(1 + np.arange(n))

ir = IsotonicRegression()
y_ = ir.fit_transform(x, y)

lr = LinearRegression()
lr.fit(x[:, np.newaxis], y)  # x needs to be 2d for LinearRegression

segments = [[[i, y[i]], [i, y_[i]]] for i in range(n)]

Automatically created module for IPython interactive environment


### Plotting Isotonic Regression¶

In [4]:
trace1 = go.Scatter(x=x, y=y, mode = 'markers',
marker = dict(
size = 10,
color = 'red'),
name="Data",
)
trace2 = go.Scatter(x=x, y=y_, mode='lines+markers',
marker = dict(
size = 10,
color = 'green'),
name="Isotonic Fit"
)
trace3 = go.Scatter(x=x, y=lr.predict(x[:, np.newaxis]),
mode = 'lines',
line = dict(
color = 'blue'),
name="Linear Fit"
)
data = []
for i in range(len(segments)):
trace4_data = go.Scatter(
x=[segments[i][0][0],segments[i][1][0]],
y=[segments[i][0][1],segments[i][1][1]],
mode = 'lines',
showlegend=False,
line = dict(
color = 'black',
width = 0.5))
data.append(trace4_data)
data.append(trace1)
data.append(trace2)
data.append(trace3)

layout = go.Layout(title = "Isotonic regression")
fig = go.Figure(data=data , layout=layout)
py.iplot(fig, filename="Isotonic_Regression")

Out[4]:

Authors:

     Nelle Varoquaux (nelle.varoquaux@gmail.com)
Alexandre Gramfort (alexandre.gramfort@inria.fr)



     BSD