Som
Self organizing Kohonen map in Python with periodic boundary conditions
Install / Use
/learn @alexarnimueller/SomREADME
som-pbc README
.. image:: https://img.shields.io/pypi/v/som-pbc.svg :target: https://pypi.org/project/som-pbc/ :alt: pypi version
.. image:: https://readthedocs.org/projects/som-pbc/badge/?version=latest :target: https://som-pbc.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status
A simple self-organizing map implementation in Python with periodic boundary conditions.
Self-organizing maps are also called Kohonen maps and were invented by Teuvo Kohonen.[1] They are an unsupervised machine learning technique to efficiently create spatially organized internal representations of various types of data. For example, SOMs are well-suited for the visualization of high-dimensional data.
This is a simple implementation of SOMs in Python. This SOM has periodic
boundary conditions and therefore can be imagined as a "donut". The
implementation uses numpy, scipy, scikit-learn and
matplotlib.
Installation
``som-pbc`` can be installed from pypi using pip::
pip install som-pbc
To upgrade ``som-pbc`` to the latest version, run::
pip install --upgrade som-pbc
Usage
~~~~~
Then you can import and use the ``SOM`` class as follows:
.. code:: python
import numpy as np
from som import SOM
# generate some random data with 36 features
data1 = np.random.normal(loc=-.25, scale=0.5, size=(500, 36))
data2 = np.random.normal(loc=.25, scale=0.5, size=(500, 36))
data = np.vstack((data1, data2))
som = SOM(10, 10) # initialize a 10 by 10 SOM
som.fit(data, 10000, save_e=True, interval=100) # fit the SOM for 10000 epochs, save the error every 100 steps
som.plot_error_history(filename='images/som_error.png') # plot the training error history
targets = np.array(500 * [0] + 500 * [1]) # create some dummy target values
# now visualize the learned representation with the class labels
som.plot_point_map(data, targets, ['Class 0', 'Class 1'], filename='images/som.png')
som.plot_class_density(data, targets, t=0, name='Class 0', colormap='Greens', filename='images/class_0.png')
som.plot_distance_map(colormap='Blues', filename='images/distance_map.png') # plot the distance map after training
# predicting the class of a new, unknown datapoint
datapoint = np.random.normal(loc=.25, scale=0.5, size=(1, 36))
print("Labels of neighboring datapoints: ", som.get_neighbors(datapoint, data, targets, d=0))
# transform data into the SOM space
newdata = np.random.normal(loc=.25, scale=0.5, size=(10, 36))
transformed = som.transform(newdata)
print("Old shape of the data:", newdata.shape)
print("New shape of the data:", transformed.shape)
Training Error:
^^^^^^^^^^^^^^^
.. figure:: https://github.com/alexarnimueller/som/blob/master/docs/_static/som_error.png?raw=true
:alt: Training Error
Point Map:
^^^^^^^^^^
.. figure:: https://github.com/alexarnimueller/som/blob/master/docs/_static/som.png?raw=true
:alt: Point Map
Class Density:
^^^^^^^^^^^^^^
.. figure:: https://github.com/alexarnimueller/som/blob/master/docs/_static/class_0.png?raw=true
:alt: Class Density Map
Distance Map:
^^^^^^^^^^^^^
.. figure:: https://github.com/alexarnimueller/som/blob/master/docs/_static/distance_map.png?raw=true
:alt: Distance Map
The same way you can handle your own data.
Methods / Functions
The SOM class has the following methods:
initialize(data, how='pca'): initialize the SOM, either via Eigenvalues (pca) or randomly (random)winner(vector): compute the winner neuron closest to a given data point invector(Euclidean distance)cycle(vector): perform one iteration in adapting the SOM towards the chosen data point invectorfit(data, epochs=0, save_e=False, interval=1000, decay='hill'): train the SOM on the givendatafor severalepochstransform(data): transform givendatain to the SOM spacedistance_map(metric='euclidean'): get a map of every neuron and its distances to all neighbors based on the neuron weightswinner_map(data): get the number of times, a certain neuron in the trained SOM is winner for the givendatawinner_neurons(data): for every data point, get the winner neuron coordinatessom_error(data): calculates the overall error as the average difference between the winning neurons and thedataget_neighbors(datapoint, data, labels, d=0): get the labels of alldataexamples that aredneurons away fromdatapointon the mapsave(filename): save the whole SOM instance into a pickle fileload(filename): load a SOM instance from a pickle fileplot_point_map(data, targets, targetnames, filename=None, colors=None, markers=None, density=True): visualize the som with all data as points around the neuronsplot_density_map(data, filename=None, internal=False): visualize the data density in different areas of the SOM.plot_class_density(data, targets, t, name, colormap='Oranges', filename=None): plot a density map only for the given classplot_distance_map(colormap='Oranges', filename=None): visualize the disance of the neurons in the trained SOMplot_error_history(color='orange', filename=None): visualize the training error history after training (fit withsave_e=True)
References:
[1] Kohonen, T. Self-Organized Formation of Topologically Correct
Feature Maps. Biol. Cybern. 1982, 43 (1), 59–69.
This work was partially inspired by `ramalina's som
implementation <https://github.com/ramarlina/som>`__ and `JustGlowing's
minisom <https://github.com/JustGlowing/minisom>`__.
Documentation:
Documentation for som-pbc is hosted on readthedocs.io <https://som-pbc.readthedocs.io/en/latest>__.
Related Skills
proje
Interactive vocabulary learning platform with smart flashcards and spaced repetition for effective language acquisition.
YC-Killer
2.7kA library of enterprise-grade AI agents designed to democratize artificial intelligence and provide free, open-source alternatives to overvalued Y Combinator startups. If you are excited about democratizing AI access & AI agents, please star ⭐️ this repository and use the link in the readme to join our open source AI research team.
best-practices-researcher
The most comprehensive Claude Code skills registry | Web Search: https://skills-registry-web.vercel.app
research_rules
Research & Verification Rules Quote Verification Protocol Primary Task "Make sure that the quote is relevant to the chapter and so you we want to make sure that we want to have it identifie
