Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hexagonal Self-Organizing map in Python

I am looking for hexagonal self-organizing map on Python.

hexagonal tiling

  1. ready module. If one exists.
  2. way to plot hexagonal cell
  3. algorithms to work with hexagonal cells as array or smth else

About: A self-organizing map (SOM) or self-organizing feature map (SOFM) is a type of artificial neural network that is trained using unsupervised learning to produce a low-dimensional (typically two-dimensional)

like image 915
Mike Avatar asked Feb 25 '10 14:02

Mike


People also ask

What is Self-Organizing Map in ML?

A self-organizing map (SOM) or self-organizing feature map (SOFM) is an unsupervised machine learning technique used to produce a low-dimensional (typically two-dimensional) representation of a higher dimensional data set while preserving the topological structure of the data.

Why do we use self organized maps SOM )?

SOM is used for clustering and mapping (or dimensionality reduction) techniques to map multidimensional data onto lower-dimensional which allows people to reduce complex problems for easy interpretation.


1 Answers

I know this discussion is 4 years old, however I haven't find a satisfactory answer over the web.

If you have something as a array mapping the input to the neuron and a 2-d array related to the location for each neuron.

For example consider something like this:

hits = array([1, 24, 14, 16,  6, 11,  8, 23, 15, 16, 15,  9, 20,  1,  3, 29,  4,
              32, 22,  7, 26, 26, 35, 23,  7,  6, 11,  9, 18, 17, 22, 19, 34,  1,
              36,  3, 31, 10, 22, 11, 21, 18, 29,  3,  6, 32, 15, 30, 27],
             dtype=int32)
centers = array([[ 1.5       ,  0.8660254 ],
                 [ 2.5       ,  0.8660254 ],
                 [ 3.5       ,  0.8660254 ],
                 [ 4.5       ,  0.8660254 ],
                 [ 5.5       ,  0.8660254 ],
                 [ 6.5       ,  0.8660254 ],
                 [ 1.        ,  1.73205081],
                 [ 2.        ,  1.73205081],
                 [ 3.        ,  1.73205081],
                 [ 4.        ,  1.73205081],
                 [ 5.        ,  1.73205081],
                 [ 6.        ,  1.73205081],
                 [ 1.5       ,  2.59807621],
                 [ 2.5       ,  2.59807621],
                 [ 3.5       ,  2.59807621],
                 [ 4.5       ,  2.59807621],
                 [ 5.5       ,  2.59807621],
                 [ 6.5       ,  2.59807621],
                 [ 1.        ,  3.46410162],
                 [ 2.        ,  3.46410162],
                 [ 3.        ,  3.46410162],
                 [ 4.        ,  3.46410162],
                 [ 5.        ,  3.46410162],
                 [ 6.        ,  3.46410162],
                 [ 1.5       ,  4.33012702],
                 [ 2.5       ,  4.33012702],
                 [ 3.5       ,  4.33012702],
                 [ 4.5       ,  4.33012702],
                 [ 5.5       ,  4.33012702],
                 [ 6.5       ,  4.33012702],
                 [ 1.        ,  5.19615242],
                 [ 2.        ,  5.19615242],
                 [ 3.        ,  5.19615242],
                 [ 4.        ,  5.19615242],
                 [ 5.        ,  5.19615242],
                 [ 6.        ,  5.19615242]])

So I'do this using a the following method:

from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

def plot_map(hits, n_centers, w=10):
    """
    Plot Map
    """

    fig = plt.figure(figsize=(w, .7 * w))
    ax = fig.add_subplot(111)
    hits_count = np.histogram(hits, bins=n_centers.shape[0])[0]
    # Discover difference between centers
    collection = RegularPolyCollection(
        numsides=6, # a hexagon 
        rotation=0, sizes=( (6.6*w)**2 ,),
        edgecolors = (0, 0, 0, 1),
        array= hits_count,
        cmap = cm.winter,
        offsets = n_centers,
        transOffset = ax.transData,
    )
    ax.axis('off')
    ax.add_collection(collection, autolim=True)
    ax.autoscale_view()
    fig.colorbar(collection)
    return ax

_ = plot_map(som_classif, matrix)

Finally I got this output:

enter image description here

EDIT

An updated version of this code on https://stackoverflow.com/a/23811383/575734

like image 74
Fernando Ferreira Avatar answered Sep 29 '22 11:09

Fernando Ferreira