# MNIST Dataset Mapping ¶

An introduction of MNIST dataset mapping using ReNom TDA.

In this tutorial, we visualize mnist dataset. you can learn following points.

• How to create topology using ReNom TDA module.
• How to understand relation between topology and mnist label value.

## Requirements ¶

In [1]:

import numpy as np

from sklearn.cluster import DBSCAN
from sklearn.datasets import fetch_mldata

from renom.tda.topology import Topology
from renom.tda.lens import PCA


## Dataset ¶

Next, we have to load-in the raw, binary MNIST data. To accomplish this, we’ll use the  fetch_mldata  module included in the scikit-learn package.

The MNIST dataset consists of 70000 digit images. Before we do anything else, we re-scale the image data (originaly integer values 0-255) to have a range from 0 to 1.

In [2]:

# Datapath must point to the directory containing the mldata folder.
data_path = "../dataset"
mnist = fetch_mldata('MNIST original', data_home=data_path)

In [3]:

data = mnist.data
target = mnist.target

# Rescale the image data to 0 ~ 1.
data = data.astype(np.float32)
data /= data.max()


## Define topology instance ¶

Next, we have to define topology instance.

ReNom TDA has two type of Topology class, Topology and SearchableTopology.

Topology class has basic four function, fit_transform, map, color and show.

SearchableTopology class extend Topology class. It has more three function, regist_categorical_data, search and get_hypercubes.

SearchableTopology can search data from categorical data.

In [4]:

topology = Topology()


## Create point cloud ¶

Next, we create point cloud that is projected on 2 or 3 dimention space.

We use fit_transform function to project data with two parameter, metric and lens.

Metric is how to measure distance between data. Lens is the axis of projected space.

This tutorial use metric None and lens PCA. This means dimenstion reduction with normal PCA.

In [5]:

metric = None
lens = [PCA(components=[0, 1])]
topology.fit_transform(data, metric=metric, lens=lens)

projected by PCA.
finish fit_transform.


## Mapping to topological space ¶

Next, we create topology.

We use map function to map point cloud to topological space.

We set three parameter, resolution, overlap and clusterer.

Resolution means the number of division. It effects the number of nodes.

Overlap means the easiness to connect with each nodes.

Clusterer means the clustering method for data that is in nodes.

In [6]:

clusterer = DBSCAN(eps=5, min_samples=5)
topology.map(resolution=50, overlap=0.3, clusterer=clusterer)

mapping start, please wait...
created 1617 nodes.
calculating cluster coordination.
calculating edge.
created 4278 edges.


## Color topology ¶

Next, we colorize topology using color funcion.

In this tutorial, topology is colored by iris label values.

We can select dtype is categorical or numerical and ctype is rgb or gray.

In [7]:

topology.color(target, dtype="categorical", ctype="rgb", normalized=False)


## Show topology ¶

Finally, we show topology.

This graph shows that data of the same label are close to each other.

In [8]:

topology.show(fig_size=(10, 10), node_size=5, edge_width=1, mode="spring", strength=0.04)