K-Means Constrained

0.9.0 · active · verified Fri Apr 17

K-Means Constrained is a Python library that implements K-Means clustering with user-defined minimum and maximum cluster size constraints. It's based on the constrained k-means algorithm by Bradley, Bennett, & Demiriz (2000). The current version is 0.9.0, and the project maintains an active but moderate release cadence, typically releasing updates a few times a year.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import `KMeansConstrained`, initialize it with cluster and size constraints, fit it to data, and access the resulting cluster labels and centers.

import numpy as np
from k_means_constrained import KMeansConstrained

# Sample data
X = np.array([
    [1, 2], [1.1, 2.1], [0.9, 1.9],
    [10, 11], [10.1, 11.1], [9.9, 10.9],
    [5, 5], [5.1, 5.1], [4.9, 4.9],
    [20, 21], [20.1, 21.1]
])

# Initialize and fit the constrained K-Means model
# n_clusters=3, min_size=2, max_size=4
clf = KMeansConstrained(
    n_clusters=3,
    size_min=2,
    size_max=4,
    random_state=0
)
clf.fit(X)

# Print cluster assignments and cluster centers
print("Labels:", clf.labels_)
print("Cluster Centers:\n", clf.cluster_centers_)

view raw JSON →