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.1.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.1-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.1-cp313-cp313-macosx_11_0_arm64.whl (95.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

silhouette_scalable-0.1.1-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.1-cp312-cp312-macosx_11_0_arm64.whl (95.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

silhouette_scalable-0.1.1-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.1-cp311-cp311-macosx_11_0_arm64.whl (94.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

silhouette_scalable-0.1.1-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.1-cp310-cp310-macosx_11_0_arm64.whl (92.8 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

silhouette_scalable-0.1.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: silhouette_scalable-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 fa7e25eeef89a4ac51da2acb93eb2c41016fb9b4b255dd6431969b19c9aff5c6
MD5 ab03124822367d33628c9d3b19f97837
BLAKE2b-256 f015be0e2e3b5b7e4f7b06ce154394b2466b59e9e69f97281b796635297b07d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1.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.1-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2919340de29ae6f83ea7232193d97c593a2c7f979608f53c7c95f90ff01dec48
MD5 64b31a594d343b99ddbf4109831ba638
BLAKE2b-256 0e9d7b3cb66c17da87d1c6862dceb460e313fe049191189d4da36659b822b55d

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d1d9281eb49438bef97ea9221a352d768bff24853418436ab037d45d7f930e0
MD5 0b4fda068e6bb83571a32dc971d4bfee
BLAKE2b-256 56ac4b493a22ce0509b3c5c1fb10c2b0a241c3684865abc87794d112a59cbaf2

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 18317512a74fbacab294af94eedab1d403d93e3b10e0a98dbfe7dc5a849ae802
MD5 87bd939533079579b52fbde27ffaa632
BLAKE2b-256 663c8be719d457000c5c633d6732ce9614a136f7edbabecb22caa6580072c182

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 48e92e6836bc4a2f74ded4ff2e2288a95e639969154d10b880b2a3eddb9d3892
MD5 fea171b4255fb944b2f02dc5ffdeab71
BLAKE2b-256 e9fa8603a766841d223590d4837df28bd7c05b7c2b1e4067d9ac5e1ba84c226b

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1251b050832cca894853b518863301099477ad7be011e391c9c3682b181c1fad
MD5 93164ce65e46b41db34aa56355e064bf
BLAKE2b-256 310deb2f0a8eec612e502628a2ab87958c78bbc8bdfc8c54c5a98635e753030b

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 af7b7ec28c150a1e9c5ba07b3806499d8981b32ddb0614d48b36d3d4f0832fad
MD5 391c9d090f2f91d065df76a2633d20c6
BLAKE2b-256 52d34a4afcf780fe05ca50b8bbc902c090c2e7204bf3027f3eac9ec2b6ebdc70

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bd2b5b1dde7a9d02d4eff9258d972c11b701d031c3394326591ef70de69ec267
MD5 77d7002f4528e256a46ec18ac97df143
BLAKE2b-256 2cd71c4ed0a3b7e10aac63f3552ec6e7e297cf7954e838d4883a22b01586f082

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79fee4fc186861376944df3ef6ffebce750e94de982f259dc54dd5f89c5ff35c
MD5 8593209383dcf901a7630bdfe748416e
BLAKE2b-256 1ba5f2131f74586157ebbdf20eb7f16f5ca3f6530bd26cfb8c8dd6725143e11d

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5c1f451c323a5762eb7f5dc0358f71762708bb36a0a77df19b973916b8b435fb
MD5 dd025978b5748525b8998d75ff4c010a
BLAKE2b-256 5d25908399c02845b0090d6855dd56ab95c53283965244fba5f4572a84ee882e

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for silhouette_scalable-0.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a722216b2a489e1717ee709638ecba59bf7a824904eaab6e5923aa4ca3fa4872
MD5 8ebd40af9377240d26be5282a47c0a8a
BLAKE2b-256 b7c82bc96ac633fb3750dc7e5f4ec55640bfb8119b23324318d6102188bda14b

See more details on using hashes here.

Provenance

The following attestation bundles were made for silhouette_scalable-0.1.1-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