Skip to main content

A Python package for both batch and incremental cluster validity indices.

Project description

cvi-header

A Python package implementing both batch and incremental cluster validity indices (CVIs).

Stable Docs Dev Docs Build Status Coverage
Stable Dev Build Status Codecov
Version Issues Downloads Zenodo DOI
version issues Downloads DOI

Table of Contents

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.

The cvi Python package contains a variety of these batch and incremental CVIs.

Installation

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

pip install cvi

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

pip install cvi==v0.6.0

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

Usage

Quickstart

Create a CVI object and compute the criterion value in batch with get_cvi:

# Import the library
import cvi
# Create a Calinski-Harabasz (CH) CVI object
my_cvi = cvi.CH()
# Load some data from some clustering algorithm
samples, labels = load_some_clustering_data()
# Compute the final criterion value in batch
criterion_value = my_cvi.get_cvi(samples, labels)

or do it incrementally, also with get_cvi:

# Datasets are numpy arrays
import numpy as np
# Create a container for criterion values
n_samples = len(labels)
criterion_values = np.zeros(n_samples)
# Iterate over the data
for ix in range(n_samples):
    criterion_values = my_cvi.get_cvi(samples[ix, :], labels[ix])

Detailed Usage

The cvi package contains a set of implemented CVIs with batch and incremental update methods. Each CVI is a standalone stateful object inheriting from a base class CVI, and all CVI functions are object methods, such as those that update parameters and return the criterion value.

Instantiate a CVI of you choice with the default constructor:

# Import the package
import cvi
# Import numpy for some data handling
import numpy as np

# Instantiate a Calinski-Harabasz (CH) CVI object
my_cvi = cvi.CH()

CVIs are instantiated with their acronyms, with a list of all implemented CVIS being found in the Implemented CVIs section.

A batch of data is assumed to be a numpy array of samples and a numpy vector of integer labels.

# Load some data
samples, labels = my_clustering_alg(some_data)

NOTE:

The cvi package 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.

You may compute the final criterion value with a batch update all at once with CVI.get_cvi

# Get the final criterion value in batch mode
criterion_value = my_cvi.get_cvi(samples, labels)

or you may get them incrementally with the same method, where you pass instead just a single numpy vector of features and a single integer label. The incremental methods are used automatically based upon the dimensions of the data that is passed.

# Create a container for the criterion value after each sample
n_samples = len(labels)
criterion_values = np.zeros(n_samples)

# Iterate across the data and store the criterion value over time
for ix in range(n_samples):
    sample = samples[ix, :]
    label = labels[ix]
    criterion_values[ix] = my_cvi.get_cvi(sample, label)

NOTE:

Currently only using either batch or incremental methods is supported; switching from batch to incremental updates with the same is not yet implemented.

Implemented CVIs

The following CVIs have been implemented as of the latest version of cvi:

  • CH: Calinski-Harabasz
  • cSIL: Centroid-based Silhouette
  • DB: Davies-Bouldin
  • GD43: Generalized Dunn's Index 43.
  • GD53: Generalized Dunn's Index 53.
  • PS: Partition Separation.
  • rCIP: (Renyi's) representative Cross Information Potential.
  • WB: WB-index.
  • XB: Xie-Beni.

History

  • 8/18/2022: Initialize project.
  • 9/8/2022: First release on PyPi and initiate GitFlow.
  • 8/10/2023: v0.5.1 released.
  • 5/31/2024: Updated 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:

Related Projects

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:

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

cvi-0.6.0.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

cvi-0.6.0-py3-none-any.whl (33.2 kB view details)

Uploaded Python 3

File details

Details for the file cvi-0.6.0.tar.gz.

File metadata

  • Download URL: cvi-0.6.0.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for cvi-0.6.0.tar.gz
Algorithm Hash digest
SHA256 202514286645b48750c8eba9b5aee135aaf7a59c1ce270301ccc7101e4a47968
MD5 be4061f09eb283bca142c023a01cc260
BLAKE2b-256 b2baa5c843cbc6e3ebc0676f927f592080a3aeb54ccd9cb25d177e00873792fd

See more details on using hashes here.

File details

Details for the file cvi-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: cvi-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 33.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for cvi-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3ad11df01cf8e7dbcbabf5d2e501b8d83cd964bde0f9bf95d5c2e2ad5621e62
MD5 97628eba79c484f83fe4289be087efe5
BLAKE2b-256 e209c5f92c6b294dd43053efebfd3e294cfa3ffbbb511a471fcde860da2491b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page