Show Sidebar Hide Sidebar

# Logistic Regression 3-Class Classifier in Scikit-learn

Show below is a logistic-regression classifiers decision boundaries on the iris dataset. The datapoints are colored according to their labels.

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

import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model, datasets


### Calculaions¶

Import some data to play with

In [3]:
iris = datasets.load_iris()
X = iris.data[:, :2]  # we only take the first two features.
Y = iris.target

h = .02  # step size in the mesh

logreg = linear_model.LogisticRegression(C=1e5)


we create an instance of Neighbours Classifier and fit the data.

In [4]:
logreg.fit(X, Y)

Out[4]:
LogisticRegression(C=100000.0, class_weight=None, dual=False,
fit_intercept=True, intercept_scaling=1, max_iter=100,
multi_class='ovr', n_jobs=1, penalty='l2', random_state=None,
solver='liblinear', tol=0.0001, verbose=0, warm_start=False)

### Plot the decision boundary.¶

In [5]:
# For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
x_ = np.arange(x_min, x_max, h)
y_ = np.arange(y_min, y_max, h)
xx, yy = np.meshgrid(x_, y_)
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])


Put the result into a color plot

In [6]:
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)

In [7]:
Z = Z.reshape(xx.shape)

trace = go.Heatmap(x=x_, y=y_, z=Z,
colorscale=cmap,
showscale=False,
)


Plot the training points

In [8]:
trace1 = go.Scatter(x=X[:, 0], y=X[:, 1],
mode='markers',
marker=dict(color=X[:, 0],
colorscale=cmap,
showscale=False,
line=dict(color='black', width=1))
)

In [9]:
layout = go.Layout(xaxis=dict(title='Sepal length', ticks='',
showticklabels=False),
yaxis=dict(title='Sepal width', ticks='',
showticklabels=False)
)

fig = go.Figure(data=[trace, trace1], layout=layout)

In [10]:
py.iplot(fig)

Out[10]:

Code source:

        GaĆ«l Varoquaux



Modified for documentation by Jaques Grobler

        BSD 3 clause