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]
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.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f00ca2b07198526711c75f6a48c10761ce51130aa13af0cef86918c6713c4322 |
|
MD5 | ce703e15c0eb62f59382b0d0e7b12a2e |
|
BLAKE2b-256 | beb092ed65050868870943064fa9d302a2c7141a68925a5c2d6b1d19892cedac |
Hashes for kmeans1d-0.2.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40546c112261b19fe762c80c087269552e135a7b93430d55d78e528a762fb579 |
|
MD5 | c5aa3db9a7b8cf7b55127d04f45cf19b |
|
BLAKE2b-256 | 5f5f299dcf0e9c0a6127bd9c45fc4e825093b421af6f8d6949f0e091ce290ece |
Hashes for kmeans1d-0.2.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcfd45913786b195bb0a6705884751d46fc449e418b7ea80eeb8f092a3096ffb |
|
MD5 | 5fd0686a170b1adb1acb36fd0c233c91 |
|
BLAKE2b-256 | 7e14ed0243c64c4ef07eeac7311dc341e979a3f525415e034e52993598009aa6 |
Hashes for kmeans1d-0.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64d3476a025d9320f86f470eb052a16fd15672314efece14bc00730f86db5a9a |
|
MD5 | cf4ff1dbebb0562d7ee06191ee257a82 |
|
BLAKE2b-256 | 20574313ea1adb348895a81da3fec2428ca93d1ff2206c74b6af556031fcd619 |
Hashes for kmeans1d-0.2.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 787eb31ec39663d3fb00423619d89dc1a3cba2dfa32d559b5da884fd27631994 |
|
MD5 | 4e19e07f4094857a57486442ebf353fd |
|
BLAKE2b-256 | 7c726c121f276b7c583671f3de0a412a03e1985fd5668f2a5b10230d27e0306d |
Hashes for kmeans1d-0.2.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76c280a5ce40053583abee1919b82fbf03d42a3493fbd0800232b1bf647eba37 |
|
MD5 | 8dc578407ccfbc75f44c3fe7bf5a2962 |
|
BLAKE2b-256 | be526d7d64609f67f07cd7cee07b29bfc5795817f754c7f741125bb2a2a03b74 |
Hashes for kmeans1d-0.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccd834c4170dba10a8fee8254e87e2109fa01e2f68aebfae27eb6a9202505c5a |
|
MD5 | 4412a52a852acad33d0241b5236d359a |
|
BLAKE2b-256 | 03f62816acdaf7184ffcc0dc55ebf337ccc4e89280bd6b7c92d160a645ed62c6 |
Hashes for kmeans1d-0.2.0-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 007a90f6d79a4e2cbc1e8c8fe32dfde28ba38e612205ac8fd7a85195a20ff4d7 |
|
MD5 | a7c6abadc7cfa6628400e04d9d15fe9f |
|
BLAKE2b-256 | 4153d1f5aa01cdaa2f54f203b9cfe373775dc7f7b6c1379e5c1b1c69e264b089 |
Hashes for kmeans1d-0.2.0-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30c5e3b411349c5f27e8975d5f130a713eb39d0e58ead5b5651682a072c0674d |
|
MD5 | 6c40fb668768c86d47b82e293b67232b |
|
BLAKE2b-256 | 663b17a559b62fed8f805045dc7d3ccad0116c3a4ee9e26b9394e0062bfa33c9 |