Metadata-Version: 2.4
Name: ds2l-som
Version: 0.3.0b1
Summary: DS2L-SOM is a Local Density-based Simultaneous Two-level Algorithm for Topographic Clustering
Author: Sandro Martens
License: MIT License
        
        Copyright (c) 2023 Sandro Martens
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE.md
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.12
Requires-Dist: dbgsom>=1.2
Requires-Dist: networkx>=3.0
Requires-Dist: numpy>=2.0
Requires-Dist: pandas>=2.0
Requires-Dist: scikit-learn>=1.5
Provides-Extra: dev
Requires-Dist: jupyter; extra == 'dev'
Requires-Dist: matplotlib; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Provides-Extra: docs
Requires-Dist: furo; extra == 'docs'
Requires-Dist: matplotlib; extra == 'docs'
Requires-Dist: numpydoc; extra == 'docs'
Requires-Dist: pillow; extra == 'docs'
Requires-Dist: scikit-learn; extra == 'docs'
Requires-Dist: sphinx; extra == 'docs'
Requires-Dist: sphinx-gallery; extra == 'docs'
Description-Content-Type: text/markdown

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

# DS2L-SOM

DS2L-SOM is a topological, density-based clustering algorithm. It combines a Self-Organizing Map (SOM) for prototype learning with Gaussian KDE density estimation and gradient ascent to detect clusters — without requiring the number of clusters to be specified in advance.

Based on the papers by Cabanes, Bennani and Fresneau (2008, 2012).

## Usage

```python
from ds2l_som.ds2lsom import DS2LSOM

model = DS2LSOM(n_prototypes=100, threshold=10)
model.fit(X)
labels = model.predict(X)   # sequential integers; -1 = unassigned

# or short:
labels = model.fit_predict(X)
```

DS2LSOM implements the scikit-learn API (`ClusterMixin`, `BaseEstimator`) and is compatible with sklearn pipelines and `GridSearchCV`.

## Parameters

| Parameter      | Default      | Description                                                       |
| -------------- | ------------ | ----------------------------------------------------------------- |
| `n_prototypes` | auto (10·√n) | Maximum number of SOM prototypes                                  |
| `threshold`    | 1            | Minimum shared samples for a prototype edge                       |
| `sigma`        | auto         | Bandwidth for Gaussian KDE density estimation                     |
| `method`       | `"som"`      | Quantizer backend: `"som"` (dbgsom) or `"kmeans"`                 |
| `model_args`   | `None`       | Kwargs passed to the quantizer: `{"init": {...}, "train": {...}}` |
| `verbose`      | `False`      | Print progress                                                    |

Example with custom SOM parameters:

```python
model = DS2LSOM(
    n_prototypes=100,
    threshold=10,
    model_args={"init": {"sigma_end": 1.0, "random_state": 42}},
)
```

## Performance

Evaluated on `load_digits` (1797 samples, 64 features, 10 classes) using pairwise Rand and Jaccard index (as defined in the papers). DS2LSOM does not receive the true number of clusters.

| Algorithm                    | Clusters | Noise | Rand  | Jaccard |
| ---------------------------- | :------: | :---: | :---: | :-----: |
| DS2LSOM                      | 9 (auto) |  37   | 0.912 |  0.461  |
| KMeans _(n=10 given)_        |    10    |   0   | 0.906 |  0.415  |
| Agglomerative _(n=10 given)_ |    10    |   0   | 0.930 |  0.542  |
| HDBSCAN                      | 6 (auto) | 1244  |   —   |    —    |

HDBSCAN metrics are excluded: 69% noise points make the scores not comparable.

## Scalability

Time and memory complexity: **O(n · k)** where k ≈ 10·√n (default heuristic), giving O(n^1.5) overall. The distance matrix (shape k × n) is the main memory bottleneck — practical limit is around **50 000–100 000 samples**.

## Installing

```bash
# Development
git clone https://github.com/SandroMartens/ds2l-som.git
cd ds2l-som
uv sync

# As a dependency in another project
uv add ds2l-som
```

## References

- _A Local Density-based Simultaneous Two-level Algorithm for Topographic Clustering_, Guénaël Cabanes and Younès Bennani, 2008
- _Enriched topological learning for cluster detection and visualization_, Guénaël Cabanes, Younès Bennani and Dominique Fresneau, 2012
