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.


Quickstart

No data download required. The example runs on a synthetic dataset generated on the fly.

pip install silhouette-scalable matplotlib seaborn scikit-learn
python examples/quickstart.py  # after cloning the repo

This will:

  1. Generate 2 000 points in 8 dimensions with 5 true clusters (make_blobs)
  2. Run k-means and estimate the silhouette for k = 2,...,8
  3. Save two plots to examples/out/:
    • silhouette_distribution.png — per-cluster silhouette distributions for each k
    • silhouette_avg_vs_k.png — average silhouette vs k (the peak identifies k = 5)

Optional arguments:

python examples/quickstart.py  # after cloning the repo --k-values 2 4 6 8 10   # custom k range
python examples/quickstart.py  # after cloning the repo --t 128                 # larger sample → more accurate estimates
python examples/quickstart.py  # after cloning the repo --plots-dir ./plots     # custom output directory

Usage

import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import silhouette_scalable as ss

# Create a dataset with real cluster structure and cluster it
n_points = 50000
X, _ = make_blobs(n_samples=n_points, n_features=16, centers=8, random_state=0)
labels = KMeans(n_clusters=8, n_init=5, random_state=0).fit_predict(X)

# Per-point silhouette estimates — O(n) in the estimation step
result = ss.compute_local(X, labels, t=64, seed=0)
print(f"Global silhouette: {result['global_silhouette']:.4f}  ({result['runtime_seconds']:.2f}s)")
print(f"First 10 local estimates: {[round(v, 3) for v in result['local_silhouette'][:10]]}")

# Fast global estimate — evaluate only m << n points, much faster when n is extremely large
result = ss.compute_global(X, labels, m=500, t=64, seed=0)
print(f"Global (fast path, m=500): {result['global_silhouette']:.4f}  ({result['runtime_seconds']:.2f}s)")

# Exact silhouette — O(n^2), reduce n_points if this is too slow on your machine
result = ss.compute_exact(X, labels)
print(f"Exact global silhouette:   {result['global_silhouette']:.4f}  ({result['runtime_seconds']:.2f}s)")

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(nkt)$ global + per-point you need per-point values
compute_global(m=m) $O(mkt)$ global only you only need the scalar, n is large
compute_global() $O(nkt)$ global only global only, same accuracy as local
compute_uniform $O(nkt)$ global + per-point uniform-sampling baseline
compute_exact $O(n^2)$ global + per-point ground truth on small datasets

We hide $log(nk/\delta)$ factor for simplicity, check the paper for the exact complexities.

For per-point silhouette distributions and best-k selection plots, see examples/quickstart.py. compute_global scales to datasets with tens of millions of points on commodity hardware.

Distances

All functions accept a distance keyword:

ss.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

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  = ss.compute_global(X, labels, m=300, seed=0)["global_silhouette"]
    print(f"k={k}  silhouette={score:.3f}")

A runnable version with per-cluster silhouette distribution plots is in examples/quickstart.py:

pip install silhouette-scalable matplotlib seaborn scikit-learn
python examples/quickstart.py  # after cloning the repo

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.3.tar.gz (622.6 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.3-cp313-cp313-manylinux_2_28_x86_64.whl (224.3 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.3-cp313-cp313-macosx_11_0_arm64.whl (95.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

silhouette_scalable-0.1.3-cp312-cp312-manylinux_2_28_x86_64.whl (224.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (95.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

silhouette_scalable-0.1.3-cp311-cp311-manylinux_2_28_x86_64.whl (223.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.3-cp311-cp311-macosx_11_0_arm64.whl (93.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

silhouette_scalable-0.1.3-cp310-cp310-manylinux_2_28_x86_64.whl (222.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.3-cp310-cp310-macosx_11_0_arm64.whl (92.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

silhouette_scalable-0.1.3-cp39-cp39-manylinux_2_28_x86_64.whl (222.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.3-cp39-cp39-macosx_11_0_arm64.whl (92.7 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: silhouette_scalable-0.1.3.tar.gz
  • Upload date:
  • Size: 622.6 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.3.tar.gz
Algorithm Hash digest
SHA256 e51adbc73e3aa0e2afd80137ed3fa8751f1536f6b062eacf42d7903508a62a4d
MD5 fd6062e2cf31f8c0bad5b55d23669dbb
BLAKE2b-256 21a8d759327d1e6cd3b787d270c7693ec423682413bd808852d2125b0a388666

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e7e1c9284fba149af8e5f071b72378ce39fda63a0bbe5b83163c0af82ec800bd
MD5 2892261119b7764d70f92bda433a4b05
BLAKE2b-256 bf3196c613ae3bedd24a1693c9148e41bdccc0345109ffdfe40093996c86d3c3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0d64851f811d51c30ea5044376ad0cef6164524eadb217f6518cc264ed3b833b
MD5 b5abcf3e5ee93d12e3357eaef1d3a12d
BLAKE2b-256 182540f32969d714ab30b30818f84a62239c22afa248a9ab8d1e79e82531738c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f5914374c2ba81a38644135b8679ce8cab5fb338cae87aec69922c91fd2bcf9d
MD5 070f601253c59ad074b07f188c644777
BLAKE2b-256 d9ec3cc07143eb2c39f99573039dc3d29dffb9118a3a74f86bacc9d9485a7b46

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b06f1ad9ec6fad24c36a94736920e111124f76a0a38eb89c283e6972c6e45442
MD5 52e00ff72c01a370b06d49ca5772ce5e
BLAKE2b-256 b0b5d17f1838f5ac0bf53d1072a06bbb426cc67e03616417a5448acba3bb81ac

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a0818285ea427d006f75e6da73e0c3881179677580b856d6ff5b577b0f8ed08a
MD5 0943c6e8a4a8135be269f9af0ebf6161
BLAKE2b-256 a067b3359c1a8ce6b560adddf472089e58a0abac11e396aac1dbc713ccaf053a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6f47b1e4bc8f188cc210b344f2d1488d9e5ab224779972903b81fe7fd85a26ef
MD5 8fc3fe561860e5a28a3f82b6c8c40b8a
BLAKE2b-256 4a9bb1f9fca268ac16efc6d96994e33b36fbac8bafae8f0cb86beea7ba710f2e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0a2c463769e4c1b268fcefe059d2fca36c0c5f7b3849dfb1dae32fb759920ff
MD5 34fab77830648c9e79a170a92c123096
BLAKE2b-256 387f86a0dc4f36aa0de0158a3ad8be966b07249b9b87f9ef242754f67871ed22

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b547fcd347461ab87b8d8b622f07a502eebb52645c30029362cbde2527f320ef
MD5 f1fc4e68e0edb71427a8cef8e682f8eb
BLAKE2b-256 ed77e04b178def350fc63553af9560d931259fd670fbe33affffcdb33464af68

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 13bc1b7bb9ddcfa942668707d1814315562b8fd5289e48616b5785d37aa003c2
MD5 283929dcbbac839a6dd2f1e78561bbd2
BLAKE2b-256 485780e89f41edc1ea59ecfe727b49f4a4fa3e7663afafb502a1b0614aa29159

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e4c2985b10fa2c002444d299c55d5dcdd6e528a243d9268875222c3687fc5e12
MD5 7d4aa5ed4d36a09728e8c02b5fedc9ff
BLAKE2b-256 b15302cf75a21a2387bb23550ab727cd600a6f176da1625e0524e8fd3d8fe432

See more details on using hashes here.

Provenance

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