Skip to main content

k-center Clustering

This is a mixed Rust/Python project: the algorithms are implemented in Rust and exposed to Python through PyO3 and maturin.

Rust is not required for normal installation on supported Linux systems. The published PyPI wheel bundles the pre-compiled Rust code together with the Python package, so users on compatible Linux systems can simply run:

pip install k-center

Algorithm

The library solves the k-center clustering problem: given $n$ points, pick $k$ cluster centers (chosen from the input points) so as to minimize the objective radius, i.e. the maximum distance between any point and its nearest center. This is an NP-hard problem in general, so the library provides approximation algorithms.

Currently only the greedy Gonzalez algorithm is implemented (algorithm="gonzalez"). The Gonzalez algorithm runs in $\mathcal{O}(k n d)$ time for $n$ points with $d$ dimensions and guarantees a 2-approximation: the resulting objective radius is at most twice the optimal value.

Supported distance metrics are euclidean, manhattan, and chebyshev.

Usage

The KCenter estimator follows the scikit-learn API (fit / predict).

import numpy as np
from k_center import KCenter

X = np.array([[0.0, 0.0], [1.0, 1.0], [10.0, 10.0], [11.0, 11.0]])

model = KCenter(n_clusters=2, distance_metric="euclidean", random_state=42)
model.fit(X)

model.labels_               # array([0, 0, 1, 1]) - cluster of each point
model.cluster_centers_      # coordinates of the two chosen centers
model.cluster_radii_        # radius of each cluster
model.objective_radius_     # the k-center objective (largest cluster radius)
model.center_indices_       # row indices of the chosen centers in X

# Assign new points to the nearest previously chosen center
model.predict([[5.0, 5.0]])

Project layout

├── Cargo.toml          # Rust crate (compiled to k_center._k_center)
│                       #   also the single source of truth for the package version
├── pyproject.toml      # Python package metadata + maturin config
│                       #   (`dynamic = ["version"]` pulls the version from Cargo.toml)
├── src/                # Rust source
│   ├── lib.rs          # crate root; registers the Python module
│   └── algorithms/     # per-algorithm Rust modules
│       ├── mod.rs
│       └── gonzalez.rs
├── python/k_center/    # pure-Python package (sklearn estimators etc.)
│   ├── __init__.py
│   └── core.py
└── tests/              # Python tests (pytest)

Development

Prerequisites

  • Rust toolchain (cargo), e.g. via rustup
  • Python 3.11+
  • uv (for the Python dev environment)

Setup

uv sync

uv sync installs packages into .venv/ but does not activate it. Run maturin/pytest through uv run (e.g. uv run maturin build) rather than relying on PATH.

Build and install

Build the wheel:

uv run maturin build

Or install directly into the current venv for development:

uv run maturin develop

Run the tests

Rust unit tests (the #[cfg(test)] blocks inside src/):

cargo test

Python tests (pytest):

uv run pytest tests/

Rust and Python developer dependencies are managed separately: Cargo dependencies live in Cargo.toml [dependencies], while Python dev tools (maturin, pytest) live in the [dependency-groups] dev group of pyproject.toml. Runtime Python dependencies (e.g. numpy, scikit-learn) go into [project] dependencies and are only recorded in the wheel metadata.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k_center-0.1.0.tar.gz (56.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl (244.0 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.28+ x86-64

File details

Details for the file k_center-0.1.0.tar.gz.

File metadata

  • Download URL: k_center-0.1.0.tar.gz
  • Upload date:
  • Size: 56.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.11 {"installer":{"name":"uv","version":"0.12.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for k_center-0.1.0.tar.gz
Algorithm Hash digest
SHA256 86899fc93c335da3c9d8e767ad4d90da8e2b81be46c54fc9adf7d72f29f1e8fa
MD5 3f20a02b7c58af2656f4016a889fa349
BLAKE2b-256 e756c230943e2aef94beb8b92fd3384993961b2415a03710f8f324fcd9ae170c

See more details on using hashes here.

File details

Details for the file k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 244.0 kB
  • Tags: CPython 3.11+, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.11 {"installer":{"name":"uv","version":"0.12.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 398aa6dfab25810e3ee0b1138ba64bc6b1d439953ca17133e30ad0ce3688a214
MD5 56faa69ed52071c59d5423330838c288
BLAKE2b-256 cb74872a9d3fa2121a5c04ef800881bc3d7fbc2829466304ca7ed308bac2e4f5

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 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