SOM tutorial with a 2D test set

Imports


In [1]:
import sys
sys.path.append('.')
sys.path.append('../')
import generatedatasets
import SOM2
import SOMclust
from pylab import *
pylab.rcParams['figure.figsize'] = 10, 10  # that's default image size for this interactive session

Generate dataset


In [2]:
xy, c = generatedatasets.dataset_fixed_cov()

In [3]:
scatter(*xy.T, c=c)


Out[3]:
<matplotlib.collections.PathCollection at 0x1015ce90>

In [4]:
xy.shape


Out[4]:
(4000, 2)

Learn


In [5]:
som = SOM2.SOM(xy)

In [6]:
smap = som.learn(verbose=True)


1 7999 8000 99.99% 1.00013637015 1.13641787895e-05 (25, 34)

and U-matrix


In [7]:
umat = som.umatrix()

In [8]:
matshow(umat)
colorbar()


Out[8]:
<matplotlib.colorbar.Colorbar instance at 0x2ba7f9260830>

Compute BMUs -- the position of each input data in the SOM


In [9]:
bmus = som.get_allbmus()

Flood the periodic map


In [10]:
clust = SOMclust.clusters(umat, bmus, som.smap)


100.00/100: flooding: 2499/2499, 1.28, (145, 160)

In [11]:
imshow(ma.masked_array(clust.umat_cont, clust.mask), interpolation='nearest')


Out[11]:
<matplotlib.image.AxesImage at 0x2ba7f9527210>