kmeans1d is a Python package providing an implementation of optimal k-means clustering specifically for one-dimensional data. It utilizes an O(kn + n log n) dynamic programming algorithm, based on research by Xiaolin (1991) and Gronlund et al. (2017), to find globally optimal k clusters. The core logic is written in C++ for performance and wrapped for Python usage. The library is actively maintained, with its current version being 0.5.0.
pip install kmeans1dVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to perform 1D k-means clustering on a sample dataset `x` with `k=4` clusters. It returns the cluster assignments for each data point and the computed centroids for each cluster.
Ensure your input data `x` is a flat list or array of numerical values representing a single dimension.
Verify that `k <= len(x)` before calling `kmeans1d.cluster(x, k)`.
For optimal compatibility across Python versions and simplified builds, ensure you are using `kmeans1d` version `0.4.0` or newer. If building from source for older versions, refer to the release notes for specific compiler or API requirements.
No dependency data recorded yet.