Show Sidebar Hide Sidebar

Randomly Generated Classification Dataset in Scikit-learn

Plot several randomly generated 2D classification datasets. This example illustrates the datasets.make_classification datasets.make_blobs and datasets.make_gaussian_quantiles functions.

For make_classification, three binary and two multi-class classification datasets are generated, with different numbers of informative features and clusters per class.

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!

Version

In [1]:
import sklearn
sklearn.__version__
Out[1]:
'0.18'

Imports

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import matplotlib.pyplot as plt

from sklearn.datasets import make_classification
from sklearn.datasets import make_blobs
from sklearn.datasets import make_gaussian_quantiles

Plot Dataset

In [3]:
fig = tools.make_subplots(rows=3, cols=2,
                          print_grid=False,
                          subplot_titles=("One informative feature, one cluster per class",
                                          "Two informative features, one cluster per class",
                                          "Two informative features, two clusters per class",
                                          "Multi-class, two informative features, one cluster",
                                          "Three blobs",
                                          "Gaussian divided into three quantiles",))
In [4]:
X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=1,
                             n_clusters_per_class=1)

one_informative = go.Scatter(x=X1[:, 0], y=X1[:, 1], 
                             mode='markers',
                             showlegend=False,
                             marker=dict(color=Y1,
                                         line=dict(color='black', width=1))
                            )

fig.append_trace(one_informative, 1, 1)

X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2,
                             n_clusters_per_class=1)

two_informative1 = go.Scatter(x=X1[:, 0], y=X1[:, 1],
                             mode='markers',
                             showlegend=False,
                             marker=dict(color=Y1,
                                         line=dict(color='black', width=1))
                            )

fig.append_trace(two_informative1, 1, 2)

X2, Y2 = make_classification(n_features=2, n_redundant=0, n_informative=2)
two_informative2 = go.Scatter(x=X2[:, 0], y=X2[:, 1],
                              mode='markers',
                              showlegend=False,
                              marker=dict(color=Y1,
                                          line=dict(color='black', width=1))
                             )


fig.append_trace(two_informative2, 2, 1)

X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2,
                             n_clusters_per_class=1, n_classes=3)

multiclass = go.Scatter(x=X1[:, 0], y=X1[:, 1],
                        mode='markers',
                        showlegend=False,
                        marker=dict(color=Y1,
                                    line=dict(color='black', width=1))
                       )

fig.append_trace(two_informative2, 2, 2)

X1, Y1 = make_blobs(n_features=2, centers=3)
three_blobs = go.Scatter(x=X1[:, 0], y=X1[:, 1], 
                         mode='markers',
                         showlegend=False,
                         marker=dict(color=Y1,
                                     line=dict(color='black', width=1))
                        )
fig.append_trace(three_blobs, 3, 1)

X1, Y1 = make_gaussian_quantiles(n_features=2, n_classes=3)
gaussian = go.Scatter(x=X1[:, 0], y=X1[:, 1], 
                      mode='markers',
                      showlegend=False,
                      marker=dict(color=Y1,
                                 line=dict(color='black', width=1))
                     )
fig.append_trace(gaussian, 3, 2)
In [5]:
fig['layout'].update(height=900)

for i in map(str, range(1, 7)):
    x = 'xaxis' + i
    y = 'yaxis' + i
    fig['layout'][x].update(zeroline=False, showgrid=False)
    fig['layout'][y].update(zeroline=False, showgrid=False)
    
py.iplot(fig)    
Out[5]:
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.