Skip to main content

Build Status

kmeans1d

A Python library with an implementation of k-means clustering on 1D data, based on the algorithm from Xiaolin (1991), as presented by Gronlund et al. (2017, Section 2.2).

Globally optimal k-means clustering is NP-hard for multi-dimensional data. Lloyd's algorithm is a popular approach for finding a locally optimal solution. For 1-dimensional data, there are polynomial time algorithms. The algorithm implemented here is an O(kn + n log n) dynamic programming algorithm for finding the globally optimal k clusters for n 1D data points.

The code is written in C++, and wrapped with Python.

Requirements

kmeans1d supports Python 3.x.

Installation

kmeans1d is available on PyPI, the Python Package Index.

$ pip3 install kmeans1d

Example Usage

import kmeans1d

x = [4.0, 4.1, 4.2, -50, 200.2, 200.4, 200.9, 80, 100, 102]
k = 4

clusters, centroids = kmeans1d.cluster(x, k)

print(clusters)   # [1, 1, 1, 0, 3, 3, 3, 2, 2, 2]
print(centroids)  # [-50.0, 4.1, 94.0, 200.5]

Tests

Tests are in tests/.

# Run tests
$ python3 -m unittest discover tests -v

Development

The underlying C++ code can be built in-place, outside the context of pip. This requires Python development tools for building Python modules (e.g., the python3-dev package on Ubuntu). gcc, clang, and MSVC have been tested.

$ python3 setup.py build_ext --inplace

The packages GitHub action can be manually triggered (Actions > packages > Run workflow) to build wheels and a source distribution.

License

The code in this repository has an MIT License.

See LICENSE.

References

[1] Wu, Xiaolin. "Optimal Quantization by Matrix Searching." Journal of Algorithms 12, no. 4 (December 1, 1991): 663

[2] Gronlund, Allan, Kasper Green Larsen, Alexander Mathiasen, Jesper Sindahl Nielsen, Stefan Schneider, and Mingzhou Song. "Fast Exact K-Means, k-Medians and Bregman Divergence Clustering in 1D." ArXiv:1701.07204 [Cs], January 25, 2017. http://arxiv.org/abs/1701.07204.

Release files for kmeans1d 0.5.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 kmeans1d 0.5.0
File Size Uploaded
kmeans1d-0.5.0.tar.gz 7.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for kmeans1d 0.5.0
File
kmeans1d-0.5.0-cp32-abi3-win_arm64.whl CPython 3.2 abi3 Windows ARM64 Details
kmeans1d-0.5.0-cp32-abi3-win_amd64.whl CPython 3.2 abi3 Windows x86-64 Details
kmeans1d-0.5.0-cp32-abi3-manylinux_2_34_x86_64.whl CPython 3.2 abi3 Linux glibc 2.34+ x86-64 Details
kmeans1d-0.5.0-cp32-abi3-manylinux_2_34_aarch64.whl CPython 3.2 abi3 Linux glibc 2.34+ ARM64 Details
kmeans1d-0.5.0-cp32-abi3-macosx_11_0_universal2.whl CPython 3.2 abi3 macOS 11.0+ universal2 (ARM64, x86-64) Details

Total release size: 306.5 kB

Release files / kmeans1d-0.5.0.tar.gz

Download URL kmeans1d-0.5.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a8fd0cda0f3d7a563d232f53c1f752850832ad920f4162dbc71040d647ba4091
BLAKE2b-256 checksum
How to use checksums
2395dc8374732aae9f0bd90c5167fdb05248be12840022307106a954376ffbfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / kmeans1d-0.5.0-cp32-abi3-win_arm64.whl

Download URL kmeans1d-0.5.0-cp32-abi3-win_arm64.whl
Size 15.9 kB
Tags CPython 3.2 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
b0cd805e8d755aed47ec35b82a16a7b27a708e8ce90ee8e54a93352fd1fe97f4
BLAKE2b-256 checksum
How to use checksums
482742ce366c599c52f49699836e33fba5ae01076931b36ef14586a6c2b7e4f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / kmeans1d-0.5.0-cp32-abi3-win_amd64.whl

Download URL kmeans1d-0.5.0-cp32-abi3-win_amd64.whl
Size 18.3 kB
Tags CPython 3.2 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
eb26be9596b9074cfca29354a86d17957f71c15a4dd0ac9d92687c3c3b6b5107
BLAKE2b-256 checksum
How to use checksums
d6af96f753a2e0dfba6d3e4a7a0626e9437d2c35104f45790d08455077b6995b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / kmeans1d-0.5.0-cp32-abi3-manylinux_2_34_x86_64.whl

Download URL kmeans1d-0.5.0-cp32-abi3-manylinux_2_34_x86_64.whl
Size 119.0 kB
Tags CPython 3.2 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
66dbb2794bc2afaec0286aaf92021b4f5557e84762b31f37dc73ae821386ea60
BLAKE2b-256 checksum
How to use checksums
6d50ca8bbf6453c1fff2f804b992f4eff00c292113bb2d8a18cb8765a05ccd92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / kmeans1d-0.5.0-cp32-abi3-manylinux_2_34_aarch64.whl

Download URL kmeans1d-0.5.0-cp32-abi3-manylinux_2_34_aarch64.whl
Size 117.0 kB
Tags CPython 3.2 Linux glibc 2.34+ ARM64 abi3
SHA-256 checksum
How to use checksums
256a86c5ca7bd9bb29d26eaa4375f8a9705ae0739cc4123494ffdc98ee918e1e
BLAKE2b-256 checksum
How to use checksums
113c2541fe9d8817b3aac12fc4d4bc1adf1c80c0b66c1b65a16ee0c47ac1dd83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / kmeans1d-0.5.0-cp32-abi3-macosx_11_0_universal2.whl

Download URL kmeans1d-0.5.0-cp32-abi3-macosx_11_0_universal2.whl
Size 28.4 kB
Tags CPython 3.2 abi3 macOS 11.0+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
3a31ed6874f1be9d2add6a18980859d3423374c471cc624098891b40c779dbf4
BLAKE2b-256 checksum
How to use checksums
eb23d39d999fc8b49e33c3de437cfcd9012ae180eb2e66eb207eacea0626c5f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.5.0 This release

6 release files

0.4.0

4 release files

0.3.1

22 release files

0.3.0

10 release files

0.1.0

1 release file

0.0.3

1 release file

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