Package: cvi
cviA Python package implementing both batch and incremental cluster validity indices (CVIs).Stable DocsDev DocsBuild StatusCoverageVersionIssuesDownloadsZenodo DOITable of ContentscviTable of ContentsCluster Validity IndicesInstallationUsageQuickstartDetailed UsageImplemented CVIsHistoryAcknowledgementsDerivationAuthorsRelated ProjectsCluster Validity IndicesSay 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 performedwell?
By definition, you wouldn't have thetruelabel 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 ametric, 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.ThecviPython package contains a variety of these batch and incremental CVIs.InstallationThecvipackage is listed on PyPI, so you may install the latest version withpipinstallcviYou can also specify a version to install in the usual way withpipinstallcvi==v0.5.1Alternatively, you can manually install a release from thereleases pageon GitHub.UsageQuickstartCreate a CVI object and compute the criterion value in batch withget_cvi:# Import the libraryimportcvi# Create a Calinski-Harabasz (CH) CVI objectmy_cvi=cvi.CH()# Load some data from some clustering algorithmsamples,labels=load_some_clustering_data()# Compute the final criterion value in batchcriterion_value=my_cvi.get_cvi(samples,labels)or do it incrementally, also withget_cvi:# Datasets are numpy arraysimportnumpyasnp# Create a container for criterion valuesn_samples=len(labels)criterion_values=np.zeros(n_samples)# Iterate over the dataforixinrange(n_samples):criterion_values=my_cvi.get_cvi(samples[ix,:],labels[ix])Detailed UsageThecvipackage contains a set of implemented CVIs with batch and incremental update methods.
Each CVI is a standalone stateful object inheriting from a base classCVI, and allCVIfunctions 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 packageimportcvi# Import numpy for some data handlingimportnumpyasnp# Instantiate a Calinski-Harabasz (CH) CVI objectmy_cvi=cvi.CH()CVIs are instantiated with their acronyms, with a list of all implemented CVIS being found in theImplemented CVIssection.A batch of data is assumed to be a numpy array of samples and a numpy vector of integer labels.# Load some datasamples,labels=my_clustering_alg(some_data)NOTE:Thecvipackage assumes the Numpyrow-majorconvention 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 withCVI.get_cvi# Get the final criterion value in batch modecriterion_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 samplen_samples=len(labels)criterion_values=np.zeros(n_samples)# Iterate across the data and store the criterion value over timeforixinrange(n_samples):sample=samples[ix,:]label=labels[ix]criterion_values[ix]=my_cvi.get_cvi(sample,label)NOTE:Currently only usingeitherbatchorincremental methods is supported; switching from batch to incremental updates with the same is not yet implemented.Implemented CVIsThe following CVIs have been implemented as of the latest version ofcvi:CH: Calinski-HarabaszcSIL: Centroid-based SilhouetteDB: Davies-BouldinGD43: 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.History8/18/2022: Initialize project.9/8/2022: First release on PyPi and initiate GitFlow.AcknowledgementsDerivationThe incremental and batch CVI implementations in this package are largely derived from the following Julia language implementations:ClusterValidityIndices.jlAuthorsThe principal authors of thecvipacakge are:Sasha Petrenkopetrenkos@mst.eduNik Meltonnmmz76@mst.eduRelated ProjectsIf this package is missing something that you need, feel free to check out some related Python cluster validity packages:validclustclusterval