Show Sidebar Hide Sidebar

# The Digit Dataset in Scikit-learn

This dataset is made up of 1797 8x8 images. Each image, like the one shown below, is of a hand-written digit. In order to utilize an 8x8 figure like this, we’d have to first transform it into a feature vector with length 64.

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

### Imports¶

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

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


### Plot Digits Dataset¶

In [3]:
digits = datasets.load_digits()


Convert matplotlib colormap to plotly.

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

In [5]:
trace = go.Heatmap(z=digits.images[-1],
colorscale=matplotlib_to_plotly(plt.cm.gray_r,
len(digits.images[-1])),
showscale=False,
)

layout = go.Layout(yaxis = dict(autorange='reversed'))
fig = go.Figure(data = [trace], layout=layout)

In [6]:
py.iplot(fig)

Out[6]:

Code source:

         Gaël Varoquaux



        BSD 3 clause