Skip to main content

Anti-clustering

Code style: black

A generic Python library for solving the anti-clustering problem. While clustering algorithms will achieve high similarity within a cluster and low similarity between clusters, the anti-clustering algorithms will achieve the opposite; namely to minimise similarity within a cluster and maximise the similarity between clusters. Currently, a handful of algorithms are implemented in this library:

  • An exact approach using a BIP formulation.
  • An enumerated exchange heuristic.
  • A simulated annealing heuristic.

Keep in mind anti-clustering is computationally difficult problem and may run slow even for small instance sizes. The current ILP does not finish in reasonable time when anti-clustering the Iris dataset (150 data points).

The two former approaches are implemented as described in following paper:
Papenberg, M., & Klau, G. W. (2021). Using anticlustering to partition data sets into equivalent parts. Psychological Methods, 26(2), 161–174. DOI. Preprint
The paper is accompanied by a library for the R programming language: anticlust.

Differently to the anticlust R package, this library currently only have one objective function. In this library the objective will maximise intra-cluster distance: Euclidean distance for numerical columns and Hamming distance for categorical columns.

Use cases

Within software testing, anti-clustering can be used for generating test and control groups in AB-testing. Example: You have a webshop with a number of users. The webshop is undergoing active development and you have a new feature coming up. This feature should be tested against as many different users as possible without testing against the entire user-base. For that you can create a maximally diverse subset of the user-base to test against (the A group). The remaining users (B group) will not test this feature. For dividing the user-base you can use the anti-clustering algorithms. A and B groups should be as similar as possible to have a reliable basis of comparison, but internally in group A (and B) the elements should be as dissimilar as possible.

This is just one use case, probably many more exists.

Installation

The anti-clustering package is available on PyPI. To install it, run the following command:

pip install anti-clustering

The package currently supports Python 3.8 and above.

Usage

The input to the algorithm is a Pandas dataframe with each row representing a data point. The output is the same dataframe with an extra column containing integer encoded cluster labels. Below is an example based on the Iris dataset:

from anti_clustering import ExactClusterEditingAntiClustering
from sklearn import datasets
import pandas as pd

iris_data = datasets.load_iris(as_frame=True)
iris_df = pd.DataFrame(data=iris_data.data, columns=iris_data.feature_names)

algorithm = ExactClusterEditingAntiClustering()

df = algorithm.run(
    df=iris_df,
    numerical_columns=list(iris_df.columns),
    categorical_columns=None,
    num_groups=2,
    destination_column='Cluster'
)

Contributions

If you have any suggestions or have found a bug, feel free to open issues. If you have implemented a new algorithm or know how to tweak the existing ones; PRs are very appreciated.

License

This library is licensed under the Apache 2.0 license.

Metadata

Release files for anti-clustering 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for anti-clustering 0.4.1
File Size Uploaded Provenance Actions
anti_clustering-0.4.1.tar.gz 13.4 kB Without provenance Details for anti_clustering-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anti-clustering 0.4.1
File Interpreter ABI Platform Actions
anti_clustering-0.4.1-py3-none-any.whl Python 3 none any Details for anti_clustering-0.4.1-py3-none-any.whl

Total release size: 33.9 kB

Release files / anti_clustering-0.4.1.tar.gz

Download URL anti_clustering-0.4.1.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
04a5adabe362dedbac4fc596885043dd2a763d03342f44127be73729a17c629f
BLAKE2b-256 checksum
How to use checksums
8b26c37c1686b7ed0ef23262eeabd2a68b960436caacf66d82d29fa8c72e576c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.11.13 Linux/6.11.0-1018-azure

Release files / anti_clustering-0.4.1-py3-none-any.whl

Download URL anti_clustering-0.4.1-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f7fc681630cee0b3b365563db522c3709f1da92e48993f397fab4c69d45a54a
BLAKE2b-256 checksum
How to use checksums
c952e958550c9c7ef70457a6f6885cea0d7ecc261858fbf7176b94a408aca0ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.11.13 Linux/6.11.0-1018-azure

Security for anti-clustering 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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