python-som
Implementation of Kohonen's 2-D self-organizing map. NumPy is the only dependency. Accepts NumPy
arrays, pandas DataFrames, polars, pyarrow, and anything else implementing the __array__ protocol.
Install
pip install python-som # requires Python 3.10+
pip install "python-som[cli]" # adds tqdm progress bars
Quick start
import numpy as np
import python_som
rng = np.random.default_rng(0)
data = rng.normal(size=(150, 4))
som = python_som.SOM(x=20, y=None, input_len=4, data=data, random_seed=42)
som.weight_initialization(mode="linear", data=data)
error = som.train(data, n_iteration=len(data), mode="batch")
umatrix = som.distance_matrix()
winner = som.winner(data[0])
A full worked example with plots is in examples/iris.py and in the getting-started guide.
Features
- Stepwise and batch training
- Random, random-sampling and linear (PCA) weight initialization
- Automatic selection of the map size ratio, from PCA
- Cyclic arrays, for toroidal maps
- Gaussian, bubble and Mexican hat neighborhood functions
- Custom decay functions
- Visualization support: U-matrix, activation matrix
- Supervised labelling, via the label map
- Fully type-annotated, with a
py.typedmarker
Neighborhood functions
All three are functions of the distance between two nodes in the grid, sqdist(c, i) in Eq. (5) of
Kohonen (2013).
| Name | Shape | Notes |
|---|---|---|
'gaussian' |
exp(-r² / 2σ²) |
Strictly positive, monotonically decreasing. The default. |
'bubble' |
1 for max(dx, dy) ≤ σ, else 0 |
The truncated inner lobe of the Mexican hat. Uses the Chebyshev metric, so the region is a square. |
'mexicanhat' |
(1 - u)·exp(-u), u = r² / 2σ² |
Excitatory near the winner, inhibitory beyond it. Zero at r = √2·σ, minimum -e⁻² at r = 2σ. |
The Mexican hat takes negative values, so it cannot be used with mode='batch': the batch
update of Kohonen Eq. (8) is a weighted mean whose denominator is not sign-definite for a signed
neighborhood function. Use mode='random' or mode='sequential'; mode='batch' raises a
ValueError.
See Neighborhood functions for the derivations, including why the Mexican hat is not an outer product of two 1-D wavelets.
Upgrading
0.3.0 corrects several methodology defects, so numerical results are not comparable with earlier
versions. In particular random_seed no longer reproduces pre-0.3.0 maps: the generator is now
per-instance rather than a call to np.random.seed on NumPy's global state. To reproduce figures
made with an older version, pin python-som==0.2.0.
Each change and the passage of Kohonen (2013) behind it is in the changelog.
Development
uv sync --all-extras
uv run pytest --cov # tests and coverage
uv run ruff check . # lint
uv run ruff format --check . # formatting
uv run mypy # type-check
uv run mkdocs serve # docs, locally
pre-commit install # optional, run the gates on commit
If you use the SonarQube for IDE (SonarLint) VS Code extension, it will also apply Sonar's Python rules locally; the ruff configuration is set up to cover most of the same ground.
References
Based on:
Teuvo Kohonen, Essentials of the self-organizing map, Neural Networks, Volume 37, 2013, Pages 52-65, ISSN 0893-6080, https://doi.org/10.1016/j.neunet.2012.09.018
The Mexican hat neighborhood follows the lateral-interaction formulation in:
O. J. Vrieze, Kohonen network, in: Artificial Neural Networks: An Introduction to ANN Theory and Practice, Lecture Notes in Computer Science, Volume 931, Springer, Berlin, Heidelberg, 1995, Pages 83-100, https://doi.org/10.1007/BFb0027024
License
MIT. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_som-0.5.0.tar.gz.
File metadata
- Download URL: python_som-0.5.0.tar.gz
- Upload date:
- Size: 161.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fdbe9f06d2b47e1008e0f152fa84b8e4ca56d9294f5bfea3c90a3df113ad918
|
|
| MD5 |
dd2a8a4e4dcbe4796eee6ee98172bace
|
|
| BLAKE2b-256 |
2c45f6616235a837e5763056b6628fb15972ddc67f988633dffdc2221dd1e22e
|
Provenance
The following attestation bundles were made for python_som-0.5.0.tar.gz:
Publisher:
release.yml on andremsouza/python-som
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_som-0.5.0.tar.gz -
Subject digest:
1fdbe9f06d2b47e1008e0f152fa84b8e4ca56d9294f5bfea3c90a3df113ad918 - Sigstore transparency entry: 2292797587
- Sigstore integration time:
-
Permalink:
andremsouza/python-som@be0ed5c6b049ca4aac6637f242056ef17951eb32 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/andremsouza
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@be0ed5c6b049ca4aac6637f242056ef17951eb32 -
Trigger Event:
push
-
Statement type:
File details
Details for the file python_som-0.5.0-py3-none-any.whl.
File metadata
- Download URL: python_som-0.5.0-py3-none-any.whl
- Upload date:
- Size: 48.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb4a70d3fd5cc094756263f85a7a6a4d8029e2d83f2d661357c8710a6dd17a91
|
|
| MD5 |
d0b81e72b348cb2cb2588719e278c66b
|
|
| BLAKE2b-256 |
65a2a7646ddc00e4ae024645f85c50a1fd6823fc93ddef405a3eb2cf4404b29b
|
Provenance
The following attestation bundles were made for python_som-0.5.0-py3-none-any.whl:
Publisher:
release.yml on andremsouza/python-som
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_som-0.5.0-py3-none-any.whl -
Subject digest:
fb4a70d3fd5cc094756263f85a7a6a4d8029e2d83f2d661357c8710a6dd17a91 - Sigstore transparency entry: 2292797871
- Sigstore integration time:
-
Permalink:
andremsouza/python-som@be0ed5c6b049ca4aac6637f242056ef17951eb32 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/andremsouza
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@be0ed5c6b049ca4aac6637f242056ef17951eb32 -
Trigger Event:
push
-
Statement type: