Skip to main content

sparse-som

PyPI License: GPL v3 DOI

Python package for the efficient Self-Organizing Map (SOM, Kohonen map) implementation of sparse-som, intended for sparse, high-dimensional data (e.g. text).

It uses an algorithm especially intended for sparse data, which is much faster than the classical one on very sparse datasets (time complexity depends on non-zero values only).

Main features

  • Highly optimized for sparse data (scipy.sparse.csr_matrix input).
  • Both online (Som) and batch (BSom) SOM algorithms.
  • Parallel batch implementation (OpenMP).
  • SomClassifier to calibrate a trained map with labels and predict.

Install

pip install sparse-som

Building from source requires a C++11 compiler with OpenMP support; pip installs the build requirements (Cython 3, numpy) itself, see pyproject.toml.

Usage

import numpy as np
from scipy.sparse import csr_matrix
from sklearn.datasets import load_digits
from sklearn.metrics import classification_report
from sparse_som import *

# Load some dataset
dataset = load_digits()

# convert to sparse CSR format
X = csr_matrix(dataset.data, dtype=np.float32)

# setup SOM dimensions
H, W = 12, 15   # Network height and width
_, N = X.shape  # Nb. features (vectors dimension)

################ Simple usage ################

# setup SOM network
som = Som(H, W, N, topology.HEXA) # , verbose=True
print(som.nrows, som.ncols, som.dim)

# reinit the codebook (not needed)
som.codebook = np.random.rand(H, W, N).\
                    astype(som.codebook.dtype, copy=False)

# train the SOM
som.train(X)

# get bmus for the data
bmus = som.bmus(X)

################ Use classifier ################

# setup SOM classifier (using batch SOM)
cls = SomClassifier(BSom, H, W, N)

# train SOM, do calibration and predict labels
y = cls.fit_predict(X, labels=dataset.target)

print('Quantization Error: %2.4f' % cls.quant_error)
print('Topographic  Error: %2.4f' % cls.topog_error)
print('='*50)
print(classification_report(dataset.target, y))

Other examples are available in the python/examples directory.

How to cite

Melka, J. and Mariage, J.-J. (2017). Efficient Implementation of Self-Organizing Map for Sparse Input Data. In Proceedings of the 9th International Joint Conference on Computational Intelligence: IJCCI, vol. 1, pp. 54-63. SciTePress. https://doi.org/10.5220/0006499500540063

BibTeX entries (including the extended 2019 Springer chapter, https://doi.org/10.1007/978-3-030-16469-0_8) are in the project README.

Development

Cython

Compilation

sparse_som/som.cpp is generated from som.pyx at build time, and not tracked.

pip install "cython>=3.0.11" numpy setuptools
python setup.py build_ext --inplace

Cleanup

python setup.py clean

Build package

python -m build

Build doc

autodoc needs the compiled package installed (Read the Docs does the same, see .readthedocs.yaml).

pip install . sphinx
sphinx-build -b html doc/source doc/build

Running tests

python -m pytest tests.py

Metadata

Release files for sparse-som 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparse-som 0.6.2
File Size Uploaded Provenance
sparse_som-0.6.2.tar.gz 19.2 kB Without provenance Details

Release files / sparse_som-0.6.2.tar.gz

Download URL sparse_som-0.6.2.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b054685430bc1ca8ced2f01f2e42ec1bac6a63ab026a15410cfa6c7598fdbacf
BLAKE2b-256 checksum
How to use checksums
17588e5378213b0e5f57e8bcad50885fa6938460d7f724c30a999ab23a3de17c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for sparse-som 0.6.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page