A Python package for optimal 1D k-means clustering
Project description
kmeans1d
A Python library with an implementation of k-means clustering on 1D data, based on the algorithm in (Xiaolin 1991), as presented in section 2.2 of (Gronlund et al., 2017).
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 a 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
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for kmeans1d-0.3.1-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b3dd21f64391d23a1799a26934b569625145dda858327c4ffc5abbb62469aab |
|
MD5 | 67d7b3cfae882e2aff598a6e3d682987 |
|
BLAKE2b-256 | bd985c6946d610e70db1bb1634e4dc2671f7eddcdc432d7c87d52b31b1e3578a |
Hashes for kmeans1d-0.3.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d019f7e7cde3bca623613a8e369dc05f67b8f8dbfb279f5ab88b25b222ee4ea |
|
MD5 | a21b3f5b4e6acd036c71ee5406ac9ea9 |
|
BLAKE2b-256 | 59ce74bdcdfa9b567b264d7925409e35622e3ec414df841095dbb9dd37696d3a |
Hashes for kmeans1d-0.3.1-cp312-cp312-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb7f4bd1706139e287512f0d7d0dc00c443b58aa95ab3e0de75c57cde78e4c7b |
|
MD5 | fd1b4585e40d27c967c443acb11c88fc |
|
BLAKE2b-256 | c99bbd29c5b01e596954459f54208dcaeb19ce0d29d38dab9868301cd7f68e07 |
Hashes for kmeans1d-0.3.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 034b82dd7ad91216a3551dffa53b11aa6870dda47641fc89073c56874a7b4015 |
|
MD5 | 6cdbbf07cea99bd4dc1b4657ba5bbdee |
|
BLAKE2b-256 | 532c07662f0b75d39d00086b7a41494d1e29fe0903db71aa1bf77e57a963f877 |
Hashes for kmeans1d-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc670fa2611a67bb8ab31734043423cb8f83dd8fb33e5b43f44070894fc207cf |
|
MD5 | b1833b0d3856884bd2829f617b7d7f38 |
|
BLAKE2b-256 | de1ae10bf87bbeeadbde7e7e3dd665ade4e6fef20ff7114b075d9ca7ea77f84e |
Hashes for kmeans1d-0.3.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfb1b94fdb930868536e9cd8c5df44aca3705b3f80d8c963cef44026c8edec8f |
|
MD5 | e04acc0dbb526dfbec2d151380c89ab1 |
|
BLAKE2b-256 | 964985bec78afcce48468af2235c2bba38dd8138876548ec58bf3cbbc4b3ee5c |
Hashes for kmeans1d-0.3.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22b052d7ee2eb6fc24d8391e3b1dcfb55335a4ed746a75cc17de7854cebf4da0 |
|
MD5 | 1c279c57a3da9f7a004f8381c536b1ee |
|
BLAKE2b-256 | 1e811444e88b846db05772fc999b3230896b5c6df5295c12ec06a1b8d7d7d09b |
Hashes for kmeans1d-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62deac12e3dce7ea869f1b8742e43845d10226eac878cc00dd5bc88cc5dc3ff2 |
|
MD5 | 6ff380d7df0f1b6fa2450f478a4aa75b |
|
BLAKE2b-256 | 59b8f2070570394b2303279d6c367f8dbccb5eed592ceebf35c719b9cbd74b6c |
Hashes for kmeans1d-0.3.1-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 406864212b3ae3ba3ebe4e10ab1d40ff12d95148da08148df8bf1a04c9254655 |
|
MD5 | 45f08a73535d4a7d0f657ffc95d4cc7f |
|
BLAKE2b-256 | 4881bf772068eda793b87a7d7fc189aa0cccc5fe7330235e5b90c7ad60247499 |
Hashes for kmeans1d-0.3.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b1f00bb9dc59625b70967ec3ddd7472039968adae2b36a973a60d91257d3c4e |
|
MD5 | 896d9b0daf30268b40a4f858aed5142f |
|
BLAKE2b-256 | 0c787378c9a497c23c2e9fd9409d201e6b110aa1d658fdb2b917bd639bd2a1fe |
Hashes for kmeans1d-0.3.1-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da880390b1419f8f2c8d0c2d6cd1794a0613006bf9b347ca677394406b832687 |
|
MD5 | 70081ce99faad90ba3442a4ff168fc68 |
|
BLAKE2b-256 | b01cf9ee0a17a46ebb956ec541daab947cad1685a462279b49cd8d8e8960929a |
Hashes for kmeans1d-0.3.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06eb99f10bbd6e7056a829ed06678aa60e911c023f6c088c66623bdd42c1f389 |
|
MD5 | c4934d05c669cf833847fc0ea9c611b1 |
|
BLAKE2b-256 | fd367a70c62380083aa9e1c79e1a078ec0148e8e368f39bc11c6a2c70870e866 |
Hashes for kmeans1d-0.3.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b62e6d5648ba1eaf9ed012528fdbd179f67cfc70fb42b0d9cae19b2a36fb4ef9 |
|
MD5 | 841e483bf75e6de5c198562544c2c6a9 |
|
BLAKE2b-256 | b38c5b3c3341fe469fdba0ad1198721f8e7c5423e72948cedfbb0a6c69c1ae17 |
Hashes for kmeans1d-0.3.1-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5015efd3d6381e61b57da033d80ae64b8ea1e06a92efbc8afbcd473ccebc80e6 |
|
MD5 | 856573a6f434b4e41f974183fd821b20 |
|
BLAKE2b-256 | 4489774ea15cf99731335b46877425cf7ea0b1bf901d77c134f0fa17e1ccd296 |
Hashes for kmeans1d-0.3.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e765af47931aa197cf7734492ac1222f7c02cbf9e1f4824ae105f8b1ac733490 |
|
MD5 | 5505be3f6bd5d4449f0a102d69b4c7af |
|
BLAKE2b-256 | ac467c2f7648340f296d3d859173036dc842a10599efc900c9814098f12a2483 |
Hashes for kmeans1d-0.3.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4953c04dfe811bc04d0898653d42dae404e42b1ad6dd70a63647bde96796a88e |
|
MD5 | 31b0653f1b96ccd8f39b03ef52f54a77 |
|
BLAKE2b-256 | d08d06edaec9ee4ce5199d69ea17991a772d376f688deadb460efe0d12aabb56 |
Hashes for kmeans1d-0.3.1-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cbb7043c9e1282e5136620840d2e92dadae0747e776e96efd8460475ae289c2 |
|
MD5 | 14f9afdd94ea2fc4fd3ec89eed90ea0d |
|
BLAKE2b-256 | f5c5c89bd26504c57f0b0c5e0dff7514e5e64d55dc9cc04d85e5ae112a9239b6 |
Hashes for kmeans1d-0.3.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80f476241037e7ff33767848a28ca8c587596828fee4031c58e1d2608ad8e357 |
|
MD5 | b424f363dab0daaa645319bacbaa9d16 |
|
BLAKE2b-256 | b815ec7669817ab574bbcd3c9e31dbad07f929c4972abfed65d8f7f4b24d5340 |
Hashes for kmeans1d-0.3.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09c6c97c3b63b5dc074c2b0a6fbc8f6e2dac252fd6bd4a7500b73fdfab5d5ef3 |
|
MD5 | 480e62f048815ecd327912db96046a4d |
|
BLAKE2b-256 | e0bc30a15f109da401fa6ef30a1632d7f0ae23a73a85a26c7c535aaa756e2144 |
Hashes for kmeans1d-0.3.1-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7362144e0b7cbe92d116c88c4f25e85a872d9ca317bb5b22c8422f8131914599 |
|
MD5 | b5befcbadeac45bc32a8c876f805cc71 |
|
BLAKE2b-256 | b73d9154a7ba17176d14c6c0e46e2e8fd53cd9645616f5ed412ca36022ba0c95 |
Hashes for kmeans1d-0.3.1-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e80bf6107ee86a3b33f6eb0e89b197f03b75d0a2f1a366cd00b6eb94801d76d1 |
|
MD5 | e292ca9cb4b93b19c3b17082d0358767 |
|
BLAKE2b-256 | 92c88883a62d0910e148163cc094c67298ea498ce15d7c2ed0801444273096c0 |