Skip to main content

kmeanssa-ng

License: MIT PyPI version Python 3.10+

Documentation Status Pipeline Status Coverage Report Code style: Ruff

K-means clustering on metric graphs and Riemannian manifolds, via simulated annealing.

kmeanssa-ng clusters data that lives on complex network structures (quantum graphs), on Riemannian manifolds such as spheres and hyperbolic space, or on any metric space where the standard Euclidean distance does not apply. Manifolds can be clustered directly, or approximated by a graph (an $\varepsilon$-net) and clustered on that.

Statement of need. Most clustering tools assume data in $\mathbb{R}^d$ with the Euclidean metric. Data on graphs, curved manifolds, or quotient spaces has no such coordinates, and the $k$-means centroid — an average — is not even defined there. kmeanssa-ng targets exactly this setting: its main algorithm is an online simulated-annealing scheme (from the companion paper) whose centres only need a Brownian motion and a drift, so it runs on any space that implements three small abstractions — Point, Center and Space. A classical Lloyd iteration is also provided as a reference method. Both plug together through interchangeable strategies (initialisation, robustification, sampling, centre update).

Installation

Install the latest version directly from PyPI:

pip install kmeanssa-ng

Quickstart

Cluster points on a quantum graph. The annealer works out of the box — k-means++ initialisation and energy-minimising robustification are the defaults:

from kmeanssa_ng import generate_sbm, SimulatedAnnealing
from kmeanssa_ng.quantum_graph.sampling import UniformNodeSampling

# A graph with two communities; pairwise distances are precomputed by default.
graph = generate_sbm(sizes=[40, 40], p=[[0.8, 0.1], [0.1, 0.8]], random_state=0)

# Sample observations to cluster (an explicit sampling strategy is required).
points = graph.sample_points(150, strategy=UniformNodeSampling(random_state=0))

# robust_prop=0.1 selects the best-of-window state (0.0, the default, would
# compare only the first and last states).
centers = SimulatedAnnealing(points, k=2, random_state=0).run(robust_prop=0.1)
print(f"Found {len(centers)} cluster centers")

Clustering on a manifold

To cluster on a curved space, approximate it by a graph (an $\varepsilon$-net) and cluster on that:

from kmeanssa_ng import (
    create_sphere, approximate_geodesic_space, FibonacciNet, SimulatedAnnealing,
)
from kmeanssa_ng.quantum_graph.sampling import UniformNodeSampling

# Approximate the 2-sphere by an epsilon-net graph, then cluster on it.
graph = approximate_geodesic_space(create_sphere(2), 500, net=FibonacciNet())
points = graph.sample_points(200, strategy=UniformNodeSampling(random_state=0))
centers = SimulatedAnnealing(
    points, k=3, beta0=0.5, step_size=0.05, random_state=0
).run(robust_prop=0.1)

Documentation

The full documentation, including API reference and tutorials, is available at kmeanssa-ng.readthedocs.io.

Citation

If you use this package in your research, please cite:

@software{kmeanssa_ng,
  author       = {Klutchnikoff, Nicolas and Gavra, Ioana},
  title        = {kmeanssa-ng: K-means Clustering on Quantum Graphs and Metric Spaces},
  year         = {2026},
  url          = {https://plmlab.math.cnrs.fr/nicolas.klutchnikoff/kmeanssa-ng},
  note         = {Python package for k-means clustering using simulated annealing}
}

A companion theoretical article is in preparation; this section will be updated with its reference once it is available.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kmeanssa_ng-0.8.0.tar.gz (113.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kmeanssa_ng-0.8.0-py3-none-any.whl (81.0 kB view details)

Uploaded Python 3

File details

Details for the file kmeanssa_ng-0.8.0.tar.gz.

File metadata

  • Download URL: kmeanssa_ng-0.8.0.tar.gz
  • Upload date:
  • Size: 113.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.28.0 CPython/3.11.15 Linux/6.12.0-219.el10.x86_64

File hashes

Hashes for kmeanssa_ng-0.8.0.tar.gz
Algorithm Hash digest
SHA256 c554d1ff84f46264d8ddf3c934fbbe361d2892bca05df666d326766a63a24c6d
MD5 23376cb778a4d1b5958622fe8f8094a7
BLAKE2b-256 05586ee9f2af174122d3122ae53b59fb46514dbb8bb381095517c629eb285fed

See more details on using hashes here.

File details

Details for the file kmeanssa_ng-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: kmeanssa_ng-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 81.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.28.0 CPython/3.11.15 Linux/6.12.0-219.el10.x86_64

File hashes

Hashes for kmeanssa_ng-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 73e734af04dd33bbd798b185f6f17c477d24a91b110e9206e3b517728e8665c9
MD5 e2634fb90883de9d2222896e95fbcb5d
BLAKE2b-256 a37eb452090523d2737838898c854e77437580a06db18037e7b659435d1384f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page