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.

Metadata

Release files for k-center 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for k-center 0.1.0
File Size Uploaded Provenance Actions
k_center-0.1.0.tar.gz 56.7 kB Without provenance Details for k_center-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for k-center 0.1.0
File Interpreter ABI Platform Actions
k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl CPython 3.11 abi3 Linux glibc 2.28+ x86-64 Details for k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl

Total release size: 300.7 kB

Release files / k_center-0.1.0.tar.gz

Download URL k_center-0.1.0.tar.gz
Size 56.7 kB
Tags Source
SHA-256 checksum
How to use checksums
86899fc93c335da3c9d8e767ad4d90da8e2b81be46c54fc9adf7d72f29f1e8fa
BLAKE2b-256 checksum
How to use checksums
e756c230943e2aef94beb8b92fd3384993961b2415a03710f8f324fcd9ae170c
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl

Download URL k_center-0.1.0-cp311-abi3-manylinux_2_28_x86_64.whl
Size 244.0 kB
Tags CPython 3.11 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
398aa6dfab25810e3ee0b1138ba64bc6b1d439953ca17133e30ad0ce3688a214
BLAKE2b-256 checksum
How to use checksums
cb74872a9d3fa2121a5c04ef800881bc3d7fbc2829466304ca7ed308bac2e4f5
Upload date
Uploaded using Trusted Publishing?
What is 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}

Security for k-center 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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