Metadata-Version: 2.1
Name: centroid-partition
Version: 1.0.0
Summary: SIMD-accelerated centroid clustering
Keywords: clustering,k-means,simd
Author-Email: Yakau Bubnou <ybubnou@gmail.com>
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: BSD License
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: System :: Clustering
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Classifier: Operating System :: MacOS
Project-URL: Funding, https://github.com/sponsors/ybubnov
Project-URL: Homepage, https://github.com/ybubnov/centroid_partition
Project-URL: Repository, https://github.com/ybubnov/centroid_partition
Requires-Python: >=3.12
Requires-Dist: numpy>=1.26
Description-Content-Type: text/markdown

# Centroid Partition - SIMD-accelerated E-means clustering

This is a SIMD-accelerated library for centroid-based clustering. The implementation is based
on E-means algorithm, introduced by Swann Bessa, et al.[^1].

The original code from the paper is distributed along with
[faiss](https://github.com/facebookresearch/faiss/tree/v1.16.0/contrib/e_means) library. The
Centroid Partition library is written to keep the library size small within a minimum set of
dependencies. The Centroid Partition library exposes both C++ and Python API.

# Installation

The C++ library could be installed into your project using `vcpkg` (release in progress).
```sh
vcpkg install centroid-partition
```

The Python bindings could be installed from PyPI like following:
```sh
pip install centroid-partition
```

## Building

The dependencies are installed by [vcpkg](https://vcpkg.io), so point `VCPKG_ROOT` to its checkout:

```sh
export VCPKG_ROOT=$HOME/vcpkg
cmake --preset=default -DCMAKE_BUILD_TYPE=Release
cmake --build build
```

## Usage

You can use the Python package `centroid_partition` to cluster the points with the `EMeans`
estimator, which follows the conventions of the scikit-learn estimators, and to find the nearest
centroids of other points.

```pycon
>>> import numpy as np
>>> from centroid_partition import EMeans
>>> X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]], dtype=np.float32)
>>> emeans = EMeans(n_clusters=2, random_state=0).fit(X)
>>> emeans.labels_
array([0, 0, 0, 1, 1, 1], dtype=int32)
>>> emeans.predict([[0, 0], [12, 3]])
array([0, 1], dtype=int32)
>>> emeans.cluster_centers_
array([[ 1.,  2.],
       [10.,  2.]], dtype=float32)
```

With the C++ library `centroid_partition` you can do the same: train the centroids on the points,
and find the nearest centroid of the points. The points and the centroids are the rows of the
row-major arrays:

```c++
#include <cstddef>
#include <random>
#include <vector>

#include <centroid_partition/centroid_partition.h>

namespace cp = centroid_partition;

int main()
{
    const std::size_t n = 10'000, dim = 16, k = 8;

    std::mt19937 rng(1);
    std::normal_distribution<float> normal;

    std::vector<float> points(n * dim);
    for (auto& value : points) {
        value = normal(rng);
    }

    // Train the centroids on the points.
    cp::centroid_partition<float> clustering(k, dim);
    std::vector<float> centroids(k * dim);
    clustering(points.data(), points.data() + points.size(), centroids.data());

    // Find the nearest centroid of the points.
    cp::centroid_nearest_search<float> search(k, dim, centroids.data());
    std::vector<decltype(search)::index_type> nearest(n);
    search(points.data(), points.data() + points.size(), nearest.data());
}
```

## License

The `centroid_partition` library is distributed under BSD 3-clause license.
See the [LICENSE](LICENSE) file for full license text.

[^1]: Swann Bessa and Pierre-Emmanuel Mazaré and Gergely Szilvasy and Hugo Touvron and
      David Picard and Matthijs Douze and Hervé Jégou -
      [e-means: outperforming k-means (in one-tenth the epochs)](https://arxiv.org/abs/2611.09501).
