Skip to main content

Composite Silhouette

Composite Silhouette is a Python package for robust clustering evaluation. It introduces a composite metric that combines micro-averaged and macro-averaged silhouette using statistical weighting. This provides a more nuanced assessment of clustering quality, helping identify the optimal number of clusters and compare performance across different clustering scenarios with greater confidence. The framework is especially useful for data scientists, ML engineers, and researchers who want reliable metrics for centroid-based clustering.

Overview and Methodology

In standard clustering evaluation, the silhouette coefficient is widely used to measure how well each data point fits into its cluster in terms of intra-cluster cohesion and inter-cluster separation. It can be aggregated as:

Composite Silhouette merges these two perspectives using a statistically-driven weighting strategy. The method performs repeated subsampled clustering to compute both micro- and macro-averaged silhouette scores. A Wilcoxon signed-rank test is then applied to their paired differences across subsamples to determine if one consistently and significantly outperforms the other. The final score is the weighted combination of the sample averages of micro- and macro-averaged scores (w · Smicro + (1-w) · Smacro). This convex combination keeps the result within the range of the individual scores and ties it meaningfully to both. When a statistically significant difference is found, the dominant metric receives at least 75% of the total weight, with the exact proportion adjusted based on the mean difference across subsamples. The greater this difference, the more the weighting shifts in its favor, while the other still contributes proportionally—reflecting the relative strength of both perspectives. If no significant difference is found, both sample-average metrics are weighted equally.

Note: The current implementation uses K-Means for clustering, which pairs well with silhouette-based evaluation and repeated subsampling. While the method can be adapted to other clustering algorithms, it already offers meaningful, statistically grounded insights for centroid-based clustering tasks.

Installation

You can install Composite Silhouette from PyPI:

pip install composite-silhouette

or directly from the GitHub repository:

pip install git+https://github.com/semoglou/composite_silhouette.git

Quick Start

from composite_silhouette import CompSil

Evaluate a Range of Cluster Counts

from sklearn.datasets import make_blobs

# Generate synthetic 2D data
X, y_true = make_blobs(n_samples=2000, centers=4, cluster_std=1.1, random_state=42)

# Initialize the Composite Silhouette evaluation
cs = CompSil(
    data=X,                        # ndarray or DataFrame
    ground_truth=len(set(y_true))  # (Optional) for visual reference in plots
    k_values=range(2, 11),         # Evaluate cluster counts from 2 to 10
    num_samples=500,               # Number of random subsamples per k
    sample_size=100,               # Number of points in each subsample
    random_state=42,               # Ensures reproducibility
    n_jobs=-1                      # Use all available CPU cores for parallel computation
)

# Run the evaluation for all specified cluster counts
cs.evaluate()

# Retrieve a DataFrame summarizing the results for each k
results_df = cs.get_results_dataframe()

# Get the k with the highest composite silhouette score
best_k = cs.get_optimal_k()

# Plot the silhouette scores and highlight the best k
cs.plot_results()

Composite Silhouette Plot

Evaluate a Single Cluster Count

cs = CompSil(
    data=X,
    k_values=4
)

cs.evaluate()

# Access the final composite silhouette score directly
score = cs.score_

# Optionally, still access the full results DataFrame
results = cs.get_results_dataframe()

Examples and Notebooks

Additional usage examples and experimental results can be found in the results/ folder:

  • example.ipynb
    Basic usage of Composite Silhouette on synthetic data.

  • performance.ipynb
    Composite silhouette evaluation results on both synthetic and real-world datasets.

These notebooks provide insight into the method's behavior and demonstrate how to apply it in practical settings.

License

This project is licensed under the MIT License.

Composite Silhouette · v0.1.0 · Last updated: 04/2025 · MIT License

Release files for composite-silhouette 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for composite-silhouette 0.1.2
File Size Uploaded
composite_silhouette-0.1.2.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for composite-silhouette 0.1.2
File Interpreter ABI Platform
composite_silhouette-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / composite_silhouette-0.1.2.tar.gz

Download URL composite_silhouette-0.1.2.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
be82db80a6187780e447704d0e4ec4766e6e4a490e3165eb2a69f12d632ebeba
BLAKE2b-256 checksum
How to use checksums
0d9abc6d5aefea4427f1fbb0ffc98b3e81e28618c404cfdf9f1c8573040dadfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 21, 2025.

Transparency log

Release files / composite_silhouette-0.1.2-py3-none-any.whl

Download URL composite_silhouette-0.1.2-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60449fa79201713e78498178602840eed8bf04e04a2c7cc0b04357746c4eb3c9
BLAKE2b-256 checksum
How to use checksums
0a6ba00e3b243524a77ce7082c7506c773899bc9c8d772c3603dd8952c49a0cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 21, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page