Skip to main content

A Python package for Optimal 1D Clustering

Project description

Build Status

optimal1dclustering

Add k-medians and add a minimum cluster size parameter to https://github.com/dstein64/kmeans1d.

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

The algorithm implemented in this library can also find the optimal clustering when clusters are required to have a minimum cluster size. It still finds the optimal clustering as the cost function is still monge concave.

Globally optimal 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

optimal1dclustering supports Python 3.x.

Installation

optimal1dclustering is available on PyPI, the Python Package Index.

$ pip3 install optimal1dclustering

Example Usage

import optimal1dclustering

x = [4.0, 4.1, 4.2, -50, 200.2, 200.4, 200.9, 80, 100, 102]
k = 4 # Number of clusters
min_cluster_size = 2 # The minimum number of elements in each cluster
mode = 2 # 2 for k-means; 1 for k-medians

clusters, centroids = optimal1dclustering.cluster(x, k, min_cluster_size = min_cluster_size, mode = mode)

print(clusters)   # [0, 1, 1, 0, 3, 3, 3, 2, 2, 2]
print(centroids)  # [-23.0, 4.15, 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.

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

optimal1dclustering-1.0.2.tar.gz (10.4 kB view hashes)

Uploaded Source

Built Distributions

optimal1dclustering-1.0.2-cp312-cp312-win_amd64.whl (20.0 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

optimal1dclustering-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (136.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

optimal1dclustering-1.0.2-cp312-cp312-macosx_10_9_universal2.whl (30.9 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

optimal1dclustering-1.0.2-cp311-cp311-win_amd64.whl (20.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

optimal1dclustering-1.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (136.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

optimal1dclustering-1.0.2-cp311-cp311-macosx_10_9_universal2.whl (30.9 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

optimal1dclustering-1.0.2-cp310-cp310-win_amd64.whl (20.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

optimal1dclustering-1.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (136.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

optimal1dclustering-1.0.2-cp310-cp310-macosx_10_9_universal2.whl (30.9 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

optimal1dclustering-1.0.2-cp39-cp39-win_amd64.whl (20.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

optimal1dclustering-1.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (135.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

optimal1dclustering-1.0.2-cp39-cp39-macosx_10_9_universal2.whl (30.9 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

optimal1dclustering-1.0.2-cp38-cp38-win_amd64.whl (20.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

optimal1dclustering-1.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (136.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

optimal1dclustering-1.0.2-cp38-cp38-macosx_11_0_universal2.whl (30.9 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ universal2 (ARM64, x86-64)

optimal1dclustering-1.0.2-cp37-cp37m-win_amd64.whl (20.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

optimal1dclustering-1.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (137.1 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

optimal1dclustering-1.0.2-cp36-cp36m-win_amd64.whl (20.1 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

optimal1dclustering-1.0.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (136.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page