<jupyter_start><jupyter_text>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](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).You can set up Plotly to work in [online](https://plot.ly/python/getting-started/initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/start-plotting-online).We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!  Version<jupyter_code>import sklearn
sklearn.__version__<jupyter_output><empty_output><jupyter_text>Imports This tutorial imports [make_blobs]() and [LogisticRegression]().<jupyter_code>print(__doc__)

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<jupyter_output>Automatically created module for IPython interactive environment<jupyter_text>Calculations<jupyter_code># 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 = np.dot(X, transformation)
fignum = 1
colors = ['blue', 'red', 'yellow']<jupyter_output><empty_output><jupyter_text>Plot Results<jupyter_code>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(plt.cm.Paired, 4)

fig = tools.make_subplots(rows=1, cols=2,
                          print_grid=False,
                          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],
                           mode='markers',
                           marker=dict(color=color,
                                       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)],
                           mode='lines', 
                           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
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_)])
fig['layout'].update(showlegend=False)

py.iplot(fig)<jupyter_output>The draw time for this plot will be slow for clients without much RAM.<jupyter_text>License Authors:         <NAME> License:        BSD 3 clause<jupyter_code>from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'Multinomial and One-vs-Rest Logistic Regression.ipynb', 'scikit-learn/plot-logistic-multinomial/', 'Multinomial and One-vs-Rest Logistic Regression | plotly',
    ' ',
    title = 'Multinomial and One-vs-Rest Logistic Regression | plotly',
    name = 'Multinomial and One-vs-Rest Logistic Regression',
    has_thumbnail='true', thumbnail='thumbnail/multinomial.jpg', 
    language='scikit-learn', page_type='example_index',
    display_as='linear_models', order=27,
    ipynb= '~Diksha_Gabha/3282')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1416709911779969, 'fraction_numerical': 0.016433143054834804, 'mean_word_length': 3.4408602150537635, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '648172', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1959, 'n_words': 436, 'path': '_posts/scikit/multinomial-and-one-vs-rest-logistic-regression/Multinomial and One-vs-Rest Logistic Regression.ipynb', 'repo_name': 'bmb804/documentation', 'size': '5792'}