Skip to main content

Scalable approximate silhouette scoring for k-clusterings under arbitrary metric distances

Project description

silhouette-scalable

Scalable approximate silhouette scoring for k-clusterings under arbitrary metric distances.

I. Sarpe, F. Altieri, A. Pietracaprina, G. Pucci, F. Vandin. Scalable and Distributed Silhouette Approximation, arXiv, 2026. Read the paper


Install

pip install silhouette-scalable

Requires Python ≥ 3.9 and NumPy. Pre-compiled wheels are available for Linux x86_64 and macOS arm64/x86_64. No C++ compiler needed.


Usage

import numpy as np
import silhouette_scalable as ss

# X: (n, d) array of points — any array-like, converted to float64
# labels: (n,) integer cluster assignments, 0-indexed
X      = np.random.randn(10_000, 32)
labels = np.random.randint(0, 10, size=10_000)

# Per-point silhouette estimates — O(n) in the estimation step
result = ss.compute_local(X, labels, distance="euclidean", t=64, seed=0)
print(result["global_silhouette"])   # float in [-1, 1]
print(result["local_silhouette"])    # list of n per-point values

# Fast global estimate — evaluate only m << n points, much faster for large n
result = ss.compute_global(X, labels, m=500, t=64, seed=0)
print(result["global_silhouette"])

# Exact silhouette — O(n²), only practical for small datasets (n ≲ 5 000)
result = ss.compute_exact(X, labels)
print(result["global_silhouette"])

Return value

All functions return a dict. The keys present depend on the function:

Key Type Present in
global_silhouette float — average silhouette score in [-1, 1] all functions
local_silhouette list[float] of length n — per-point scores compute_local, compute_uniform, compute_exact
runtime_seconds float — wall-clock time of the C++ call all functions

When to use which function

Function Cost Returns Use when
compute_local O(n) global + per-point you need per-point values
compute_global(m=m) O(m) global only you only need the scalar, n is large
compute_global() O(n) global only global only, same accuracy as local
compute_uniform O(n) global + per-point uniform-sampling baseline
compute_exact O(n²) global + per-point ground truth on small datasets

Distances

All functions accept a distance keyword:

sa.compute_local(X, labels, distance="manhattan")

Supported: "euclidean" (default), "sqeuclidean", "manhattan", "cosine", "canberra".

Key parameters

Parameter Default Description
t 64 PPS sample size per cluster — larger gives tighter estimates
delta 0.01 Failure probability for the approximation guarantee
m n (compute_global only) number of points to evaluate
threads 1 OpenMP thread count (Linux wheels only)
seed 100 RNG seed for reproducibility
k auto Number of clusters — inferred as max(labels)+1 if not set

Approximation guarantees

Given a clustering $\mathcal{C} = {C_1, \dots, C_k}$ of a dataset $V = {e_1,\dots,e_n}$, the silhouette of a point $e \in C$ is

$$ s(e) = \frac{b(e)-a(e)}{\max{a(e), b(e)}}, \qquad a(e) = \frac{\sum_{e' \in C} d(e,e')}{|C|-1}, \qquad b(e) = \min_{C_j \neq C}\frac{\sum_{e' \in C_j}d(e,e')}{|C_j|}, $$

and the average silhouette is $s(\mathcal{C}) = \frac{1}{n}\sum_{e\in V} s(e)$.

compute_local and compute_global implement the PPS-weighted estimator $\hat{s}_2$ from the paper:

$$\Pr!\left[|\hat{s}_2 - s(\mathcal{C})| \le \tfrac{4\varepsilon}{1-\varepsilon}\right] > 1 - \delta$$

with $O!\left(\frac{nk}{\varepsilon^2}\log\frac{nk}{\delta}\right)$ distance computations.


Best-k selection example

import silhouette_scalable as ss
from sklearn.datasets import make_blobs

X, _ = make_blobs(n_samples=5_000, centers=5, n_features=8, random_state=0)

for k in range(2, 10):
    from sklearn.cluster import KMeans
    labels = KMeans(n_clusters=k, n_init=5, random_state=0).fit_predict(X)
    score  = sa.compute_global(X, labels, m=300, seed=0)["global_silhouette"]
    print(f"k={k}  silhouette={score:.3f}")

A runnable version with plots is in examples/quickstart.py.


Research / HPC usage

The repository also contains the full research codebase used to produce the paper results, including HDF5-based experiment pipelines and an Apptainer container for HPC clusters.

Build the C++ binary

Linux (Ubuntu/Debian)

sudo apt install g++ make libhdf5-dev nlohmann-json3-dev
cd cppCode && make
conda env create -f research/environment.yml && conda activate silhouetteEnv

macOS (requires Homebrew)

brew install hdf5 libomp nlohmann-json
cd cppCode && make
conda env create -f research/environment.yml && conda activate silhouetteEnv

Apptainer (recommended for HPC / reproducibility)

apptainer build image.sif image.def   # build once from repo root
sbatch slurm_launcher.slurm           # compiles C++ on first run

Reproducing paper results

The full experiment pipeline (HDF5 datasets, cluster scripts, result aggregation) is available in the tagged research release: v1.0-paper

Project details


Download files

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

Source Distribution

silhouette_scalable-0.1.0.tar.gz (623.2 kB view details)

Uploaded Source

Built Distributions

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

silhouette_scalable-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl (224.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.0-cp313-cp313-macosx_11_0_arm64.whl (95.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

silhouette_scalable-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl (224.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (95.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

silhouette_scalable-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl (223.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (94.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

silhouette_scalable-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl (222.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (92.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

silhouette_scalable-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl (222.4 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.0-cp39-cp39-macosx_11_0_arm64.whl (92.9 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file silhouette_scalable-0.1.0.tar.gz.

File metadata

  • Download URL: silhouette_scalable-0.1.0.tar.gz
  • Upload date:
  • Size: 623.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for silhouette_scalable-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4558e26d53066d90e35708bf51d988c347af1207983108952ca2a86fa0d24cc4
MD5 421f7aa2f436f49c51b49f5cfdf22eda
BLAKE2b-256 b890f2c4173f3ef164703482b4db2823a104e3edfbe98d962385dd5509135281

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0.tar.gz:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e408bfd6bc3c0d29a5f834cb0d3380a5bea1350185576d087c2c26bd7b282b10
MD5 0addbb3a69cf2ca757835642fe30b036
BLAKE2b-256 24ff1d7eda9444a946fa808263eb88f90c13296c62c970e43568da117893571b

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp313-cp313-manylinux_2_28_x86_64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dd79bbf569d7368504e4bdf65d20170a05ef8715e7c82c6ed5713135eb955ff7
MD5 e7b2e02122a8cfd9549e9b0d167a2eff
BLAKE2b-256 fa3f41d233c1c2b2b7b81abf17c33f4a1761702a200e15248521b42e7c745566

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d4687072410cd470b6d595a2e29c508e6eb4706af30a2c4382fbd2a763113f3c
MD5 739e7dcdc0b5c6d46b7453ac93d1823e
BLAKE2b-256 422bf4867cf1b61a56edd99752d1d31aab03d8059a621a15fa8c21622732c655

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6b9bf75b7d60bd0f04b0069e8526205b2f4b87a1233ded198622e5aec2fdde2e
MD5 c7b397b6a5a7dad7c1f049a3e6051283
BLAKE2b-256 f07949290774909ee594933e18c5ae88573b020a77a4659e466f1060d6f31759

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3c5b7dd7804fe00908b8bf7a450e0387e79c6203c0e1c6eb75233a31ade6f264
MD5 06cccbf79ca7e3e9bbe6c7f353e1ffa4
BLAKE2b-256 655d0398b9dc732712243bcd9d1ee820c6a08d1bd295563a2051be76f89a05f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 04fee05dc201a470f166660fdcfd72545550078938d4a4b416c338cc5d8728e2
MD5 8c7ed6c351e3475a1934937435a9de89
BLAKE2b-256 6b3ca37ce30196d94ceea5ab62401a8f9da31944dea4cdc045ab97c3bc7d1601

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e3c7bdea16eb80a73adf547fdc4ee087a8096edaeb248c88388b22f264df7a78
MD5 708314629bec6596629e542404ab45a8
BLAKE2b-256 fc677cb5a42af424b87fe19c59e7f52023d8b0434f3dd5f320fa664487e2e1a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0690ca673e2ed69663a6e0320ccc2861dd03373c6073f1cd366daf7a7a5975a1
MD5 7166d82d59f68c6c00d805ee14fb6647
BLAKE2b-256 816a15bf42cbb04a5a87f5afedd896075c533be618e87b240f1dbdb07377f8cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b9450fc6ba414ab332410c449fe605d31b1feb7525b0fedb883708819998ebd4
MD5 15daee721d75148d93f08d318ab85ae4
BLAKE2b-256 dc6a196912d20d413631d053e0fa51d4aab443f60a4f01ea2bc198af26501bdd

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file silhouette_scalable-0.1.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f4a2105fcacb3c3f4bda1ad8bae99c435def94ee40540684d773cd46d964dc31
MD5 66ac9671be153629c5c9c3b37983d8bb
BLAKE2b-256 8869667fc95ecea35de416b436d0117e6101e09cc91d52233d5b5dda5751cebd

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.0-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: release.yml on iliesarpe/ScalableSilhouetteComputation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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