skillfed

k-means-constrained

K-Means clustering constrained with minimum and maximum cluster size

k-means-constrained v0.9.1 122.6K downloads/30d#11,943 on PyPI236
Permissive license BSD 3-Clause Active released

What it is and what it does

k-means-constrained is a variant of k-means clustering that enforces minimum and maximum size constraints on each cluster. Instead of the standard assignment step, it formulates cluster assignment as a minimum cost flow optimization problem solved by ortools' cost-scaling push-relabel algorithm. This ensures that every cluster respects the specified size bounds while minimizing overall distance, making it useful when balanced or size-controlled partitioning is required.

The package implements a scikit-learn-compatible API and depends on ortools, scipy, numpy, six, and joblib. It trades computational speed for constraint satisfaction: the time complexity is substantially higher than vanilla k-means, scaling as O((n³c + n²c² + nc³)log(n+c)) versus O(nc) for standard k-means. The package is actively maintained, supports Python 3.10–3.13 with recent free-threading beta support, and has no known vulnerabilities.

Use it for:

  • Partition customer data into balanced segments for fair resource allocation or stratified analysis
  • Create evenly-sized geographic clusters or facility assignments where each region must serve a minimum/maximum population
  • Generate balanced train/test splits or cross-validation folds with size guarantees
  • Cluster time-series or sensor data where each cluster must contain a minimum viable sample size for statistical validity

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

K-means clustering with enforced minimum and maximum cluster sizes, using a minimum cost flow algorithm to solve the constrained assignment step.

Yes, if you need size-constrained clustering and can tolerate higher computational cost. The package is stable (Production/Stable status), actively maintained, permissively licensed, and has no security vulnerabilities. Install friction is medium due to compiled wheels, but pre-built binaries for modern Python versions (3.10–3.13) and multiple platforms are available. Not recommended if you need vanilla k-means performance on large datasets or if cluster size constraints are not a hard requirement.

Install

k-means-constrained on PyPI

pip

pip install k-means-constrained

uv

uv add k-means-constrained

poetry

poetry add k-means-constrained

Installing k-means-constrained

Before you install

Medium install friction due to compiled wheels across multiple Python versions and platforms. Active maintenance with recent release (40 days ago); repository shows steady development with 236 stars and no archived status.

License in practice

BSD 3-Clause permissive license allows commercial and private use with minimal restrictions; attribution and license notice required in distributions.

Quickstart

pip install k-means-constrained
from k_means_constrained import KMeansConstrained
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
clf = KMeansConstrained(n_clusters=2, size_min=2, size_max=5, random_state=0)
clf.fit_predict(X)

Verify before relying

  • Whether performance degradation at scale is acceptable for your data size and cluster count
  • Compatibility with free-threaded Python (3.14t) given ortools' GIL re-enablement behavior

Package facts

License BSD 3-Clause (permissive)
Python support not specified
Install friction medium — platform-specific wheel
Runtime dependencies 5 — ortools, scipy, numpy, six, joblib
Maintenance actively maintained — 40 days since the last release
Last repo commit
First released
Downloads 122,624/month — #11,943 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: k_means_constrained-0.9.1-cp310-cp310-macosx_10_9_x86_64.whl; k_means_constrained-0.9.1-cp310-cp310-macosx_11_0_arm64.whl; k_means_constrained-0.9.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; k_means_constrained-0.9.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; k_means_constrained-0.9.1-cp310-cp310-win_amd64.whl; k_means_constrained-0.9.1-cp311-cp311-macosx_10_9_x86_64.whl; k_means_constrained-0.9.1-cp311-cp311-macosx_11_0_arm64.whl; k_means_constrained-0.9.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; k_means_constrained-0.9.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; k_means_constrained-0.9.1-cp311-cp311-win_amd64.whl; k_means_constrained-0.9.1-cp312-cp312-macosx_10_13_x86_64.whl; k_means_constrained-0.9.1-cp312-cp312-macosx_11_0_arm64.whl; k_means_constrained-0.9.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; k_means_constrained-0.9.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; k_means_constrained-0.9.1-cp312-cp312-win_amd64.whl; k_means_constrained-0.9.1-cp313-cp313-macosx_10_13_x86_64.whl; k_means_constrained-0.9.1-cp313-cp313-macosx_11_0_arm64.whl; k_means_constrained-0.9.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl; k_means_constrained-0.9.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl; k_means_constrained-0.9.1-cp313-cp313-win_amd64.whl

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseProgramming Language :: Python :: 3Programming Language :: Python :: Free Threading :: 2 - BetaTopic :: Scientific/Engineering

Tags

constrained k-means clusteringk-means with cluster size limitsminimum maximum cluster sizebalanced clustering algorithmconstrained clustering python
clusteringconstrained-optimizationscikit-learn-compatible

More Scientific/Engineering packages