py-self-organizing-map
Simple implementation of Self Organizing Maps (SOMs) with rectangular and hexagonal grid topologies. A SOM is a simple unsupervised method to learn a mapping from a source space to a typically two-dimensional target space. It starts with an initial neighborhood graph (either with a rectangular or hexagonal topology) where each node is associated with a weight vector (same number of components as source space). Then, iteratively, a "random" sample from the dataset is chosen and the node with the best matching weight w.r.t. some distance metric is determined. The weights of this best matching node and its neighbors are then slightly dragged towards the sample vector. This procedure is repeated for a certain number of iterations such that over time more and more nodes are positioned in high-density regions of the dataset while the neighborhood relation leads to a relatively smooth mapping.
There are quite a few hyperparameters such as height
and width
of the discrete grid, the initialization
, the distance_metric
, the topology
, the number ofepochs
or the initial_radius
. All of these have a large impact on the resulting map, so please feel free to play around with it.
from som import SelfOrganizingMap
# create a random set of RGB color vectors
N = 1000
X = np.random.randint(0, 255, (N, 3))
# create the SOM and fit it to the color vectors
s = SelfOrganizingMap(height=32, width=32, topology='rectangular', initialization='random_uniform', distance_metric='l2')
s.fit(X, epochs=10, lr_decay=0.1, radius_decay=0.1, initial_radius=4)
# plot the learned map
f = plt.figure()
ax1 = f.add_subplot(121)
ax2 = f.add_subplot(122)
s.plot_som(ax1)
s.plot_node_difference_map(ax2)
plt.show()