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

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 --k-values 2 4 6 8 10   # custom k range
python examples/quickstart.py --t 128                 # larger sample → more accurate estimates
python examples/quickstart.py --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

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.2.tar.gz (622.5 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.2-cp313-cp313-manylinux_2_28_x86_64.whl (224.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

silhouette_scalable-0.1.2-cp312-cp312-manylinux_2_28_x86_64.whl (224.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (95.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

silhouette_scalable-0.1.2-cp311-cp311-manylinux_2_28_x86_64.whl (223.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.2-cp311-cp311-macosx_11_0_arm64.whl (93.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

silhouette_scalable-0.1.2-cp310-cp310-manylinux_2_28_x86_64.whl (221.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

silhouette_scalable-0.1.2-cp310-cp310-macosx_11_0_arm64.whl (92.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

silhouette_scalable-0.1.2-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.2-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.2.tar.gz.

File metadata

  • Download URL: silhouette_scalable-0.1.2.tar.gz
  • Upload date:
  • Size: 622.5 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.2.tar.gz
Algorithm Hash digest
SHA256 234c5bf8a3d8d8e78b61f98dfc8592ef9ea27cfba2a966966b16edc280c74d27
MD5 81a6d4b2078b1b5f03696504809bf500
BLAKE2b-256 6915c6b617b74ac9eac3d502f62ca854ba21d1ce493c886e9425fd5eb13703aa

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 24de1fb172fd546f10dee2d6f5198120aecf8ccb6c4bbb9bb53b72e52c80ef95
MD5 c10ac488549b64250ac3891ba25826c1
BLAKE2b-256 04902d3c57c4ee6469ddfdc1b0a2cc9d2c97ffec0ad3a6920c906192115a930a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 85db7ce5e8ef50e979958d6068488033a36ecea66475e8d981c1760aebcdaa63
MD5 e34f424f9a9a809e26a333a26d034557
BLAKE2b-256 96419748d5adc107afdf4c98c8e9fcb3d19edf560e34395b8d74ccdb09280c84

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5a5f92c82348d58d354527d3f4ffa083d00aa9a2573603a94bb9ad93359545e5
MD5 ab65803eb3a48587996a2d9d18bac9bf
BLAKE2b-256 db70f604c970b8ef64148b0841234da4c9452c5ba2eacf9db000fafe216329f0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5e3a4af3df14cc23cf1caf0f423cecc8de8720d8ae60ed02ccb1185ade9d4916
MD5 3662fe012a25b1c692114d3af8db661d
BLAKE2b-256 4ebe7ecfa36f3afa547e6b2442e15ee7ca2fff82b870d15744956df7016f30ca

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 103ce4f0157f51e9c5d8968d1246078ae905ac3ef85cfa93b7729a52e2eda689
MD5 69ceea0634561e8405a0602ffa989bca
BLAKE2b-256 82c2aa25efb5fc9b5c2cb6de746126bf18459f909612f5dd7a0e5fc02355d326

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1541cd21230a902a64f1d012fde2d2ede7129484cca0adce7a7ba96ebb1c557a
MD5 b7670f78608b32d1c08e25e601ceedf8
BLAKE2b-256 c1d4cb2303e7a543155a6ee7c8793965c24f669ef4b702f090db99646fca122c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 494b35801b8ec4fbc0be5814a8fc9fa4d883f826748b8bacdb009414bec5601b
MD5 b8f31d699e108d157a51a7e185a2e5d4
BLAKE2b-256 4219229f9358c3bf4cdfd5473b829ee362ae78b634add645bdd27a7cafef2554

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7394feda56b80c4258dc412ca51950ed2735ae5a29467da320404339b19e15b0
MD5 824a7be99bc910d8c54f2ce33e3bdd93
BLAKE2b-256 f5108557fd11a3890be7aafa360b36f2e0a12128b3168e8fda35f1e5182294ae

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 45cead0b9535748eea55161d1bd4cb68f8565eb4d14ebc3735b4d131e7588411
MD5 3ec9d9cd5be2c2b261449e34a7e53e8f
BLAKE2b-256 3fbeecdaf439d0e8b2ec13d355133335285cceb1270deb647788a3e3377c327f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for silhouette_scalable-0.1.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 74fd5cd8ec745d03d312c04485b1204bf8819bf80d26b1562bb52863a57609dd
MD5 04dfc3d6d3edb0550a0f8598b9b8976a
BLAKE2b-256 fd63c449ec20ab1b8f0e41cf50a1698ac1d313ae372fd1bd59477b4ec23148b1

See more details on using hashes here.

Provenance

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