Self organizing maps in 3d
See original GitHub issueHi, your script of the self organizing maps looks really amazing! I tried extending it to 3D and visualize it with vtkplotter, this is what i get:
which looks meaningful, yet I’m not completely sure if that’s what one would expect to see (I might have messed up the indeces…). Does it make sense to you?
If so, may I include your script as an the example usage of the vtkplotter package?
# -----------------------------------------------------------------------------
# Copyright 2019 (C) Nicolas P. Rougier
# Released under a BSD two-clauses license
#
# References: Kohonen, Teuvo. Self-Organization and Associative Memory.
# Springer, Berlin, 1984.
# https://github.com/rougier/ML-Recipes/blob/master/recipes/ANN/som.py
# -----------------------------------------------------------------------------
import numpy as np
import scipy.spatial
np.random.seed(0)
class SOM:
""" Self Organizing Map """
def __init__(self, shape, distance):
""" Initialize som """
self.codebook = np.random.uniform(0, 1, shape)
self.labels = np.random.uniform(0, 1, len(self.codebook))
self.distance = distance / distance.max()
def learn(self, samples, n_epoch=10000, sigma=(0.25, 0.01), lrate=(0.5, 0.01)):
""" Learn samples """
t = np.linspace(0, 1, n_epoch)
lrate = lrate[0] * (lrate[1] / lrate[0]) ** t
sigma = sigma[0] * (sigma[1] / sigma[0]) ** t
I = np.random.randint(0, len(samples), n_epoch)
samples = samples[I]
for i in range(n_epoch):
# Get random sample
data = samples[i]
# Get index of nearest node (minimum distance)
winner = np.argmin(((self.codebook - data) ** 2).sum(axis=-1))
# Gaussian centered on winner
G = np.exp(-self.distance[winner] ** 2 / sigma[i] ** 2)
# Move nodes towards sample according to Gaussian
self.codebook -= lrate[i] * G[..., np.newaxis] * (self.codebook - data)
def sample_spherical(npoints, ndim=3):
vec = np.random.randn(ndim, npoints)
vec /= np.linalg.norm(vec, axis=0)
return vec.T
# -----------------------------------------------------------------------------
if __name__ == "__main__":
from vtkplotter import Points, Line, show
n = 16
ls = np.linspace(0, 1, n)
X, Y, Z = np.meshgrid(ls, ls, ls)
P = np.c_[X.ravel(), Y.ravel(), Z.ravel()]
D = scipy.spatial.distance.cdist(P, P)
som = SOM((len(P), 3), D)
samples = sample_spherical(2500)
# samples = P # this looks reasonable
som.learn(samples, n_epoch=10000, sigma=(0.5, 0.01), lrate=(0.5, 0.01))
# Draw samples
actors = [Points(samples, c="lb", alpha=0.2)]
# Draw network
x, y, z = [som.codebook[:, i].reshape(n, n, n) for i in range(3)]
for k in [0, 8, 15]:
for i in range(n):
ptjs = []
for j in range(n):
ptjs.append((x[i, j, k], y[i, j, k], z[i, j, k]))
actors.append(Line(ptjs)) # line through a serie of 3d points
for j in range(n):
ptjs = []
for i in range(n):
ptjs.append((x[i, j, k], y[i, j, k], z[i, j, k]))
actors.append(Line(ptjs))
show(actors, axes=8)
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Cluster and visualize data using 3D self-organizing maps
Abstract— The Self Organizing Maps (SOM) can be considered as an excellent computational tool and has been applied in numerous application areas. The...
Read more >The Ultimate Guide to Self Organizing Maps (SOM's) - Blogs
The Ultimate Guide to Self Organizing Maps (SOM's) · Step One: Choose the number of clusters. · Step Two: Randomly select K points...
Read more >Implementing in-situ self-organizing maps with memristor ...
A self-organizing map (SOM) is a powerful unsupervised learning neural network for analyzing high-dimensional data in various applications.
Read more >[PDF] On the use of Three-dimensional Self-Organizing Maps ...
This paper shows that the 3D SOM can be used successfully for visualizing clusters in georeferenced data and points to a significant increase...
Read more >Cluster and visualize data using 3D self-organizing maps
The Self Organizing Maps (SOM) can be considered as an excellent computational tool and has been applied in numerous application areas.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi again @rougier , I felt the urge of making a fancy visualization of the maze solver:
https://github.com/marcomusy/vtkplotter/tree/master/examples/other
I’m still amazed of how well this MDP thing can do the job!! Best wishes Marco
…indeed with meshes it looks nicer: https://github.com/marcomusy/vtkplotter/tree/master/examples/other 😃