Skip to main content

Constrained KMeans

Modified version of KMeans algorithm that takes into account partial information about the data.

Given a partial list of known labels init_labels, Constrained KMeans finds a cluster configuration that complies with init_labels. init_labels is the same length as x.shape[0], which is why a second array can_change masks out which labels should be marked as known and which labels can change. Formally, the output of the algorithm is an array labels such that np.all((labels[can_change == 0] == init_labels[can_change == 0])) is True.

Can be installed via (requires Python>=3.7)

pip install ConstrainedKMeans

Example basic usage:

import numpy as np
from matplotlib import pyplot as plt

from ConstrainedKMeans import ConstrainedKMeans as CKM

def run_test(n_points):
    ckm = CKM(n_clusters=10)

    # Generate random dataset
    # For visualization purposes, initialize 2d data
    x = np.random.random((n_points, 2))
    # Generate random labels
    init_labels = np.random.randint(0, 10, n_points)
    # Generate 0s with probability 0.2
    # these shall mask the "known" labels
    can_change = np.random.binomial(2, 0.7, n_points)

    labels = ckm.fit_predict(x, can_change, init_labels)

    plt.scatter(x[:, 0], x[:, 1], c=labels)
    plt.show()

if __name__ == '__main__':
    run_test(1000)

Metadata

Release files for ConstrainedKMeans 1.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 ConstrainedKMeans 1.2
File Size Uploaded
ConstrainedKMeans-1.2.tar.gz 225.3 kB Details

Built distribution (wheel)

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

Total release size: 232.2 kB

Release files / ConstrainedKMeans-1.2.tar.gz

Download URL ConstrainedKMeans-1.2.tar.gz
Size 225.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0bef4a7f228f085df27efce292f6be5ca701bb4068e0f428c8c2ef8794b885e8
BLAKE2b-256 checksum
How to use checksums
385c4c98cfb35f6a475ae99d2dfe99fae6e2fa9796170148d100bae1a5ee829a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

Release files / ConstrainedKMeans-1.2-py3-none-any.whl

Download URL ConstrainedKMeans-1.2-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ffec8ed669abbdcb97bc474c420df369a7bd9c47a07ce2a7c91459f0a70f6ef
BLAKE2b-256 checksum
How to use checksums
4244b5a8c92be6db6dcda8eafd9b878a9ba3a4f5c8ef805e3796ca14c1236d9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

Release history Release notifications | RSS feed

This release

1.2 This release

2 release files

1.1

2 release files

1.0

2 release files

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