Registry / ai-ml / k-means-constrained

k-means-constrained

JSON →
library0.9.0pypypi✓ verified 85d ago

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.

pip install k-means-constrained
INSTALL
IMPORT
SIG · K-MEANS-CONSTRAINE
K
k-means-constrained
ai-mlpythonv0.9.0
Install
13.6s avg
Import
1716ms
Disk
393MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
build_error
glibc
py 3.103.910 runs
installs and imports cleanly · install 13.6s · import 1.373s · 387MB
393MB installed
● package 393MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

KMeansConstrained
from k_means_constrained import KMeansConstrained

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_)
Debug
Known issues
gotchaComputational complexity increases significantly with large datasets, many clusters, or very tight cluster size constraints. The constrained K-Means problem is NP-hard.
fix
For very large datasets, consider pre-processing steps like dimensionality reduction or data sampling. Carefully choose `n_clusters`, `size_min`, and `size_max` to balance model requirements with computational feasibility.
affects: All versions
gotchaResults are non-reproducible without setting `random_state`. The initialization of cluster centers and subsequent iterative steps can involve randomness.
fix
Always set the `random_state` parameter in the `KMeansConstrained` constructor (e.g., `random_state=42`) to ensure deterministic and reproducible output across runs.
affects: All versions
gotchaIncompatible cluster constraints (`n_clusters`, `size_min`, `size_max`) can lead to a `ValueError` or an unsolvable problem, as the algorithm cannot partition the data as requested.
fix
Ensure that the total number of samples (`n_samples`) can be consistently partitioned: `n_clusters * size_min <= n_samples <= n_clusters * size_max`. Adjust parameters if an error indicates an impossible configuration.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'k_means_constrained'
The `k-means-constrained` package is not installed in your Python environment or the import path is incorrect.
fix
Install the package using `pip install k-means-constrained`. Ensure your import statement is `from k_means_constrained import KMeansConstrained`.
ValueError: Not enough points to satisfy cluster constraints.
The total number of samples (`n_samples`) provided to `fit` is incompatible with the specified `n_clusters`, `size_min`, and `size_max` parameters.
fix
Verify that `n_clusters * size_min <= n_samples <= n_clusters * size_max`. Adjust the number of clusters, min/max sizes, or provide more data points to satisfy the constraints.
TypeError: size_min must be an integer
The `size_min` or `size_max` parameter was provided as a float or other non-integer type.
fix
Ensure that both `size_min` and `size_max` are integer values when initializing `KMeansConstrained`.
Upgrade
Version history
0.9.0latest on PyPI · released Jan 27, 2026
Audit
Dependencies
numpyrequiredNumerical operations and array manipulation.
scikit-learnrequiredCore machine learning utilities and base K-Means functionality.
scipyrequiredScientific computing routines, potentially for optimization.
Agent activity
6 hits · last 30 days
node
6
Resources
k-means-constrained — pip install k-means-constrained · libregistry