digits exampleΒΆ

In [1]:
# import data
import numpy as np
from sklearn import datasets
data, labels = datasets.load_digits().data, datasets.load_digits().target
In [2]:
from seaborn import plt

u_labels = np.unique(labels)
f, axs = plt.subplots(2,5, figsize=(12,6))
flat_axs = axs.flatten()
for u_label in u_labels:
    flat_axs[u_label].imshow(
        data[np.where(labels == u_label)[0][0]].reshape((8,8)))
    flat_axs[u_label].set_title("number {}".format(u_label))
    flat_axs[u_label].set_axis_off()

f
/home/travis/miniconda/envs/cartographer/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/home/travis/miniconda/envs/cartographer/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Out[2]:
../_images/examples_digits_example_2_1.png
In [3]:
from sklearn.manifold import TSNE
tsne = TSNE().fit_transform(data)
In [4]:
fig, ax = plt.subplots()
ax.scatter(tsne[:,0],tsne[:,1],c=labels)
ax.set_title("TSNE")
fig
Out[4]:
../_images/examples_digits_example_4_0.png
In [5]:
from cartographer.mapper import Mapper
from cartographer.coverers import HyperRectangleCoverer
from sklearn.preprocessing import FunctionTransformer
from cartographer.visualization import html_graph
from IPython.core.display import HTML
from sklearn.cluster import DBSCAN

m = Mapper(coverer=HyperRectangleCoverer(intervals=20, overlap=0.6),
           filterer=FunctionTransformer(), # does nothing
           clusterer=DBSCAN(min_samples=7,eps=0.4))
m.fit(tsne)

HTML(html_graph(m, {"labels" : labels},
                { "tsne_x" : tsne[:,0],
                  "tsne_y" : tsne[:,1]}))
Out[5]: