Show Sidebar Hide Sidebar

Multinomial and One-vs-Rest Logistic Regression in Scikit-learn

Plot decision surface of multinomial and One-vs-Rest Logistic Regression. The hyperplanes corresponding to the three One-vs-Rest (OVR) classifiers are represented by the dashed lines.

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!


In [1]:
import sklearn


This tutorial imports make_blobs and LogisticRegression.

In [2]:

import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.linear_model import LogisticRegression
Automatically created module for IPython interactive environment


In [3]:
# make 3-class dataset for classification
centers = [[-5, 0], [0, 1.5], [5, -1]]
X, y = make_blobs(n_samples=1000, centers=centers, random_state=40)
transformation = [[0.4, 0.2], [-0.4, 1.2]]
X =, transformation)
fignum = 1
colors = ['blue', 'red', 'yellow']

Plot Results

In [4]:
def matplotlib_to_plotly(cmap, pl_entries):
    h = 1.0/(pl_entries-1)
    pl_colorscale = []
    for k in range(pl_entries):
        C = map(np.uint8, np.array(cmap(k*h)[:3])*255)
        pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])
    return pl_colorscale

cmap = matplotlib_to_plotly(, 4)
In [5]:
fig = tools.make_subplots(rows=1, cols=2,
                          subplot_titles=('Decision surface of LogisticRegression (multinomial)',
                                          'Decision surface of LogisticRegression (ovr)'))

for multi_class in ('multinomial', 'ovr'):
    clf = LogisticRegression(solver='sag', max_iter=100, random_state=42,
                             multi_class=multi_class).fit(X, y)

    # print the training scores
    print("training score : %.3f (%s)" % (clf.score(X, y), multi_class))

    # create a mesh to plot in
    h = .02  # step size in the mesh
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    x_ = np.arange(x_min, x_max, h)
    y_ =  np.arange(y_min, y_max, h)
    xx, yy = np.meshgrid(x_, y_)

    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    trace = go.Contour(x=x_, y=y_, z=Z, 
                       colorscale=cmap, showscale=False)
    fig.append_trace(trace, 1, fignum)
    # Plot also the training points
    for i, color in zip(clf.classes_, colors):
        idx = np.where(y == i)
        trace = go.Scatter(x=X[idx, 0][0], y=X[idx, 1][0],
                                       line=dict(color='black', width=1))
        fig.append_trace(trace, 1, fignum)

    # Plot the three one-against-all classifiers
    coef = clf.coef_
    intercept = clf.intercept_

    def plot_hyperplane(c, color):
        def line(x0):
            return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]
        trace = go.Scatter(x=[x_min, x_max], y=[line(x_min), line(x_max)],
                           line=dict(color=color, dash='dash'))
        return trace

    for i, color in zip(clf.classes_, colors):
        trace = plot_hyperplane(i, color)
        fig.append_trace(trace, 1, fignum)
    fignum += 1
training score : 0.995 (multinomial)
training score : 0.976 (ovr)
In [6]:
fig['layout']['xaxis1'].update(range=[min(x_), max(x_)])
fig['layout']['xaxis2'].update(range=[min(x_), max(x_)])
fig['layout']['yaxis1'].update(range=[min(y_), max(y_)])
fig['layout']['yaxis2'].update(range=[min(y_), max(y_)])




    Tom Dupre la Tour <>


    BSD 3 clause