A Python package implementing batch and incremental cluster validity indices (CVIs) for hard partitions.
| Stable Docs | Dev Docs | Build Status | Coverage |
|---|---|---|---|
| Version | Issues | Downloads | Zenodo DOI |
Cluster validity indices measure properties such as compactness, separation, and connectivity when ground-truth labels are unavailable. This package uses a shared, stateful interface for evaluating a complete labeled partition or tracking its criterion value as samples arrive.
Please see the documentation for detailed usage.
- Table of Contents
- What Are Cluster Validity Indices?
- Installation
- Quickstart
- Implemented Indices
- Updating an Existing Partition
- Acknowledgements
Say you have a clustering algorithm that clusters a set of samples containing features of some kind and some dimensionality. Great! That was a lot of work, and you should feel accomplished. But how do you know that the algorithm performed well? By definition, you wouldn't have the true label belonging to each sample (if one could even exist in your context), just the label prescribed by your clustering algorithm.
Enter Cluster Validity Indices (CVIs).
CVIs are metrics of cluster partitioning when true cluster labels are unavailable. Each operates on only the information available (i.e., the provided samples of features and the labels prescribed by the clustering algorithm) and produces a metric, a number that goes up or down according to how well the CVI believes the clustering algorithm appears to, well, cluster. Clustering well in this context means correctly partitioning (i.e., separating) the data rather than prescribing too many different clusters (over partitioning) or too few (under partitioning). Every CVI itself also behaves differently in terms of the range and scale of their numbers. Furthermore, each CVI has an original batch implementation and incremental implementation that are equivalent.
The cvi package is listed on PyPI, so you may install the latest version with
python -m pip install cviYou can also specify a version to install in the usual way with
pip install cvi==0.7.0Alternatively, you can manually install a release from any of the builds on the releases page on GitHub.
import numpy as np
import cvi
samples = np.array([
[0.0, 0.1],
[0.2, 0.0],
[2.8, 3.0],
[3.1, 2.9],
])
labels = np.array([0, 1, 2, 2])
# Batch evaluation
batch_index = cvi.CH()
batch_value = batch_index.get_cvi(samples, labels)
# Incremental evaluation
incremental_index = cvi.CH()
values = np.empty(len(labels))
for i, (sample, label) in enumerate(zip(samples, labels)):
values[i] = incremental_index.get_cvi(sample, int(label))CVI objects accumulate state. Use a fresh object for each independent dataset or partition. A batch call may be followed by incremental samples, but the same object cannot be initialized with a second batch.
[!NOTE] NOTE The
cvipackage assumes the Numpy row-major convention where rows are individual samples and columns are features. A batch dataset is then[n_samples, n_features]large, and their corresponding labels are[n_samples]large.
Users can also query the .info property of the CVI objects to obtain relevant scaling and naming information.
>>> print(my_cvi.info)
CVIInfo(name='Calinski-Harabasz', name_short='CH', index_min=0.0, index_max=inf, optimality='max')
| Index | Prefer | Range | Batch | Incremental | Remove/merge |
|---|---|---|---|---|---|
CH |
Larger | [0, ∞) |
Yes | Yes | Yes |
CONN |
Larger | [0, 1] |
Yes | Fuzzy backend only | No |
cSIL |
Larger | [-1, 1] |
Yes | Yes | Yes |
DB |
Smaller | [0, ∞) |
Yes | Yes | Yes |
GD43 |
Larger | [0, ∞) |
Yes | Yes | Yes |
GD53 |
Larger | [0, ∞) |
Yes | Yes | Yes |
PS |
Larger | [0, 1] |
Yes | Yes | Yes |
rCIP |
Smaller | [0, ∞) |
Yes | Yes | Yes |
WB |
Smaller | [0, ∞) |
Yes | Yes | Yes |
XB |
Smaller | [0, ∞) |
Yes | Yes | Yes |
CONN uses prototype connectivity and has additional backend and normalization requirements.
See the CONN guide before using it.
Except for CONN, initialized indices support adding samples, removing samples, and merging clusters without replaying the full dataset via remove and merge:
value = index.get_cvi(new_sample, new_label)
value = index.remove(existing_sample, existing_label)
value = index.merge(target_label=20, source_label=10)Both methods update the object in place and return its new criterion value.
Removing the final sample of a cluster deletes that cluster, while merge retains target_label and deletes source_label.
The caller is responsible for ensuring that a removed sample belongs to the supplied label.
For input rules, index-selection guidance, references, legacy API information, and the complete API, see the documentation.
The incremental and batch CVI implementations in this package are largely derived from the following Julia language implementations by the same authors of this package:
The principal authors of the cvi pacakge are:
- Sasha Petrenko petrenkos@mst.edu
- Nik Melton nmmz76@mst.edu
If this package is missing something that you need, feel free to check out some related Python cluster validity packages:
The following font is used in the logo:
The icon for the project is taken from:
