fast1dkmeans
A Python library which implements several variations of optimal k-means clustering on 1D data, based on the algorithms presented by Gronlund et al. (2017). This package is inspired by the kmeans1d package but extends it by implementing additional algorithms, in particular those with reduced memory requirements O(n) instead of O(kn).
There are several different ways to compute the optimal k-means clustering in 1d. Currently the package implements the following methods:
"binary-search-interpolation"default [O(n lg(U) ), O(n) space, "wilber-interpolation"]"dynamic-programming-kn"[O(kn), O(kn) space]"dynamic-programming-space"[O(kn), O(n) space, "dp-linear"]"binary-search-normal"[O(n lg(U) ), O(n) space, section 2.4, "wilber-binary"]
The code is written in Python and relies on the numba compiler for speed.
Requirements
fast1dkmeans relies on numpy and numba which currently support python 3.8-3.10.
Installation
fast1dkmeans is available on PyPI, the Python Package Index.
$ pip3 install fast1dkmeans
Example Usage
import fast1dkmeans
x = [4.0, 4.1, 4.2, -50, 200.2, 200.4, 200.9, 80, 100, 102]
k = 4
clusters = fast1dkmeans.cluster(x, k)
print(clusters) # [1, 1, 1, 0, 3, 3, 3, 2, 2, 2]
Important notice: On first usage the the code is compiled once which may take about 30s. On subsequent usages this is no longer necessary and execution is much faster.
Tests
Tests are in tests/.
# Run tests
$ python3 -m pytest .
License
The code in this repository has an BSD 2-Clause "Simplified" License.
See LICENSE.
References
[1] 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 fast1dkmeans 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fast1dkmeans-0.1.2.tar.gz | 14.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fast1dkmeans-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 34.4 kB
Release files / fast1dkmeans-0.1.2.tar.gz
| Download URL | fast1dkmeans-0.1.2.tar.gz |
|---|---|
| Size | 14.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
02a7295fb415895ccc35b7b4dde9882ae541caac0258f05ab9e63bac05598998
|
|
BLAKE2b-256 checksum How to use checksums |
227cbf37b9e4217457d3a4e3709b552a6ebc6fce77f95e4ef0a11bdb1c7be493
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.2
|
Release files / fast1dkmeans-0.1.2-py3-none-any.whl
| Download URL | fast1dkmeans-0.1.2-py3-none-any.whl |
|---|---|
| Size | 20.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b255507d7801a809e11fc6b3b23281e643683b5da645a4086f6ebb6c4c98d24
|
|
BLAKE2b-256 checksum How to use checksums |
f2dbd3639c5177e0cfa173cf6cfc22c8244614deca1aab7ea1ad84b7b62d46fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.2
|