Skip to main content

cvi-header

A Python package implementing batch and incremental cluster validity indices (CVIs) for hard partitions.

Stable Docs Dev Docs Build Status Coverage
Stable Dev Build Status Codecov
Version Issues Downloads Zenodo DOI
version issues Downloads 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?

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.

Installation

The cvi package is listed on PyPI, so you may install the latest version with

python -m pip install cvi

You can also specify a version to install in the usual way with

pip install cvi==0.7.2

Alternatively, you can manually install a release from any of the builds on the releases page on GitHub.

Quickstart

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.

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')

Implemented Indices

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.

Updating an Existing Partition

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.

Acknowledgements

Derivation

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:

Authors

The principal authors of the cvi pacakge are:

If this package is missing something that you need, feel free to check out some related Python cluster validity packages:

Assets

Fonts

The following font is used in the logo:

Icons

The icon for the project is taken from:

Release files for cvi 0.7.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 cvi 0.7.2
File Size Uploaded
cvi-0.7.2.tar.gz 45.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cvi 0.7.2
File Interpreter ABI Platform
cvi-0.7.2-py3-none-any.whl Python 3 none any Details

Total release size: 91.8 kB

Release files / cvi-0.7.2.tar.gz

Download URL cvi-0.7.2.tar.gz
Size 45.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c5e7996455ba8f417de1e8e6aac7bd8a490a6fd8c3c9f0493a105fe6ac09b4d7
BLAKE2b-256 checksum
How to use checksums
9ffd711926c050b383e1d60e41e746fb7453c63958b373e506bc5f83951d33ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / cvi-0.7.2-py3-none-any.whl

Download URL cvi-0.7.2-py3-none-any.whl
Size 46.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cd4bb1211ce276c5aa6b5bad76c752711d92fb9ffa0d8854f63b4e9e50551dc
BLAKE2b-256 checksum
How to use checksums
5e07de081f283171a12fb90e66ca2a03e8606088dcd5a1164ab7e31ac1b5db86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7
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