# Fast self-organizing maps in Python with Somoclu

*Posted on 30 September 2015*

## Introduction¶

Self-organizing maps are computationally intensive to train, especially if the original space is high-dimensional or the map is large. Very large maps where the number of neurons is at least five times the number of data points are sometimes called emergent-self organizing maps -- these are especially demanding to train. Somoclu is a highly efficient, parallel and distributed algorithm to train such maps, and its Python interface was recently updated. This enables fast training of self-organizing maps on multicore CPUs or a GPU from Python, albeit only on dense data, and the distributed computing capability is also not exposed. The Python interface also lets you process the output files of the command-line version, so if the data is sparse or the map was trained on a cluster, you can still use the module for visualization. Here we take a quick look at how to train and visualize a small map.

First, we import the necessary modules:

```
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import somoclu
%matplotlib inline
```

Then we generate and plot some random data in three categories:

```
c1 = np.random.rand(50, 3)/5
c2 = (0.6, 0.1, 0.05) + np.random.rand(50, 3)/5
c3 = (0.4, 0.1, 0.7) + np.random.rand(50, 3)/5
data = np.float32(np.concatenate((c1, c2, c3)))
colors = ["red"] * 50
colors.extend(["green"] * 50)
colors.extend(["blue"] * 50)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(data[:, 0], data[:, 1], data[:, 2], c=colors)
labels = range(150)
```

## Planar maps¶

We train Somoclu with default parameter settings, asking for a large map that qualifies as an emergent self-organizing map for this data:

```
n_rows, n_columns = 100, 160
som = somoclu.Somoclu(n_columns, n_rows, data=data)
%time som.train()
```

We plot the component planes of the trained codebook of the ESOM:

```
som.view_component_planes()
```

We can plot the U-Matrix, together with the best matching units for each data point. We color code the units with the classes of the data points and also add the labels of the data points.

```
som.view_umatrix(bestmatches=True, bestmatchcolors=colors, labels=labels)
```

We can also zoom into a region of interest, for instance, the dense lower right corner:

```
som.view_umatrix(bestmatches=True, bestmatchcolors=colors, labels=labels,
zoom=((50, n_rows), (100, n_columns)))
```

## Toroid topology, hexagonal grid¶

We can repeat the above with a toroid topology by specifying the map type as follows:

```
som = somoclu.Somoclu(n_columns, n_rows, data=data, maptype="toroid")
som.train()
som.view_umatrix(bestmatches=True, bestmatchcolors=colors)
```

Notice how the edges of the map connect to the other side. Hexagonal neurons are also implemented:

```
som = somoclu.Somoclu(n_columns, n_rows, data=data, gridtype="hexagonal")
som.train()
som.view_umatrix(bestmatches=True, bestmatchcolors=colors)
```

The separation of the individual points is more marked with these neurons.

## Initialization with principal component analysis and clustering the results¶

We can pass an initial codebook of our choice, but we can also ask Somoclu to initialize the codebook with vectors from the subspace spanned by the first two eigenvalues of the correlation matrix. To do this, we need to pass an optional argument to the constructor:

```
som = somoclu.Somoclu(n_columns, n_rows, data=data, maptype="toroid", initialization="pca")
som.train()
som.view_umatrix(bestmatches=
```