MiniSom is a minimalistic and Numpy based implementation of the Self Organizing Maps (SOM). SOM is a type of Artificial Neural Network able to convert complex, nonlinear statistical relationships between high-dimensional data items into simple geometric relationships on a low-dimensional display. Minisom is designed to allow researchers to easily build on top of it and to give students the ability to quickly grasp its details.
The project initially aimed for a minimalistic implementation of the Self-Organizing Map (SOM) algorithm, focusing on simplicity in features, dependencies, and code style. Although it has expanded in terms of features, it remains minimalistic by relying only on the numpy library and emphasizing vectorization in coding style.
Updates about MiniSom are posted on X.
Jump into using MiniSom via Google Colab:
Just use pip:
pip install minisom
or download MiniSom to a directory of your choice and use the setup script:
git clone https://github.com/JustGlowing/minisom.git
python setup.py install
In order to use MiniSom you need your data organized as a Numpy matrix where each row corresponds to an observation or as list of lists like the following:
data = [[ 0.80, 0.55, 0.22, 0.03],
[ 0.82, 0.50, 0.23, 0.03],
[ 0.80, 0.54, 0.22, 0.03],
[ 0.80, 0.53, 0.26, 0.03],
[ 0.79, 0.56, 0.22, 0.03],
[ 0.75, 0.60, 0.25, 0.03],
[ 0.77, 0.59, 0.22, 0.03]]
Then you can train MiniSom just as follows:
from minisom import MiniSom
som = MiniSom(6, 6, 4, sigma=0.3, learning_rate=0.5) # initialization of 6x6 SOM
som.train(data, 100) # trains the SOM with 100 iterations
You can obtain the position of the winning neuron on the map for a given sample as follows:
som.winner(data[0])
For an overview of all the features implemented in minisom you can browse the following examples: https://github.com/JustGlowing/minisom/tree/master/examples
A model can be saved using pickle as follows
import pickle
som = MiniSom(7, 7, 4)
# ...train the som here
# saving the som in the file som.p
with open('som.p', 'wb') as outfile:
pickle.dump(som, outfile)
and can be loaded as follows
with open('som.p', 'rb') as infile:
som = pickle.load(infile)
Note that if a lambda function is used to define the decay factor MiniSom will not be pickable anymore.
You can use this dashboard to explore the effect of the parameters on a sample dataset: https://share.streamlit.io/justglowing/minisom/dashboard/dashboard.py
Here are some of the charts you'll see how to generate in the examples:
Seeds map | Class assignment |
Handwritten digits mapping | Hexagonal Topology |
Color quantization | Outliers detection |
@misc{vettigliminisom,
title={MiniSom: minimalistic and NumPy-based implementation of the Self Organizing Map},
author={Giuseppe Vettigli},
year={2018},
url={https://github.com/JustGlowing/minisom/},
}
MiniSom has been cited more than 300 times, check out the research where MiniSom was used here.
pytest
can be used for this:
pytest minisom.py
pycodestyle
:
pycodestyle minisom.py