Skip to main content

Fast and explainable clustering based on sorting

Project description

CLASSIX

Fast and explainable clustering based on sorting

Build Status codecov License: MIT PyPI pyversions !pypi Documentation Status Anaconda-Server Badge Anaconda-Server Badge Anaconda-Server Badge Binder

CLASSIX is a fast and explainable clustering algorithm based on sorting. Here are a few highlights:

  • A novel clustering algorithm which exploits the sorting of data points.
  • Ability to cluster low and high-dimensional data of arbitrary shape efficiently.
  • Ability to detect and deal with outliers in the data.
  • Ability to provide textual explanations for the generated clusters.
  • Full reproducibility of all tests in the accompanying paper.
  • Support of Cython compilation.

CLASSIX is a contrived acronym of CLustering by Aggregation with Sorting-based Indexing and the letter X for explainability. CLASSIX clustering consists of two phases, namely a greedy aggregation phase of the sorted data into groups of nearby data points, followed by a merging phase of groups into clusters. The algorithm is controlled by two parameters, namely the distance parameter radius for the aggregation and a minPts parameter controlling the minimal cluster size.

Here is a video abstract of the CLASSIX: Watch this simple tutorial video

The detailed documentation, including tutorials, is available at Dev

Install

CLASSIX requires the following essential dependencies for clustering:

  • cython>=0.29.4
  • numpy>=1.20.0
  • scipy>1.6.0
  • requests

while requires the following dependencies for data visualization:

  • matplotlib
  • pandas

To install the current release via PIP use:

pip install ClassixClustering

For conda environment, to install this package with conda run:

conda install -c nla.stefan.xinye classix

Download this repository via:

$ git clone https://github.com/nla-group/classix.git

Quick Start

from sklearn import datasets
from classix import CLASSIX

# Generate synthetic data
X, y = datasets.make_blobs(n_samples=1000, centers=2, n_features=2, random_state=1)

# Employ CLASSIX clustering
clx = CLASSIX(sorting='pca', radius=0.5, verbose=0)
clx.fit(X)

Get the clustering result by clx.labels_ and visualize the clustering:

plt.figure(figsize=(10,10))
plt.rcParams['axes.facecolor'] = 'white'
plt.scatter(X[:,0], X[:,1], c=clx.labels_)
plt.show()

The explain method

CLASSIX provides an API for the easy visualization of clusters, and to explain the assignment of data points to their clusters. Let us demonstrate this functionality with some simple data. To get an overview of all the data, the location of starting points, and their associated groups, simply type:

clx.explain(plot=True)

The starting points are marked as the small red boxes (the color can be specified by user). If plot=True is omitted, only a textual summary will be produced.

The output summarizes the computed groups and clusters:

A clustering of 5000 data points with 2 features has been performed. 
The radius parameter was set to 0.50 and MinPts was set to 0. 
As the provided data has been scaled by a factor of 1/6.01,
data points within a radius of R=0.50*6.01=3.01 were aggregated into groups. 
In total 7903 comparisons were required (1.58 comparisons per data point). 
This resulted in 14 groups, each uniquely associated with a starting point. 
These 14 groups were subsequently merged into 2 clusters. 
A list of all starting points is shown below.
----------------------------------------
 Group  NrPts  Cluster  Coordinates 
   0     398      0     -1.19 -1.09 
   1    1073      0     -0.65 -1.15 
   2     553      0     -1.17 -0.56 
   3     466      0     -0.67 -0.65 
   4       6      0     -0.19 -0.88 
   5       3      0     -0.72 -0.03 
   6       1      0     -0.22 -0.28 
   7     470      1       0.31 0.21 
   8     675      1       0.18 0.71 
   9     579      1       0.86 0.19 
  10     763      1       0.69 0.67 
  11       6      1       0.42 1.35 
  12       5      1       1.24 0.59 
  13       2      1        1.0 1.08 
----------------------------------------
In order to explain the clustering of individual data points, 
use .explain(ind1) or .explain(ind1, ind2) with indices of the data points. 

In the columns of the above table, Group denotes the group label, NrPts denotes the number of data points in the associated group, Cluster is the cluster label assigned to the corresponding group, and Coordinates are the coordinates of starting point associated with the group. In order to explain the cluster assignment of a particular data point, we just have to provide its index to the explain method:

clx.explain(0,  plot=True)
Output:
The data point 0 is in group 2, which has been merged into cluster #0.

You can also track and compare the two objects merging and division with

clx.explain(0, 2000,  plot=True)
Output:
The data point 0 is in group 2, which has been merged into cluster 0.
The data point 2000 is in group 10, which has been merged into cluster 1.
There is no path of overlapping groups between these clusters.

Citation

Here is the full CLASSIX paper. If you find CLASSIX useful in a scientific publication, we would appreciate a citation:

@misc{CLASSIX,
      title={Fast and explainable clustering based on sorting}, 
      author={Xinye Chen and G\"{u}ttel, Stefan},
      year={2022},
      eprint={},
      archivePrefix={arXiv},
      primaryClass={}
}

Project details


Release history Release notifications | RSS feed

This version

0.2.8

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ClassixClustering-0.2.8.tar.gz (383.4 kB view hashes)

Uploaded Source

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