Metadata-Version: 2.5
Name: keo
Version: 0.1.5
Summary: KEO: reproducible benchmark of K-means (Lloyd), E-means and O-means
Project-URL: Homepage, https://github.com/rajveer43/keo
Project-URL: Repository, https://github.com/rajveer43/keo
Project-URL: Issues, https://github.com/rajveer43/keo/issues
Project-URL: Changelog, https://github.com/rajveer43/keo/blob/main/CHANGELOG.md
Project-URL: Citation, https://doi.org/10.5281/zenodo.24273473
Author: Rajveer Rathod
License: MIT
License-File: LICENSE
Keywords: benchmark,clustering,e-means,embeddings,faiss,glove,k-means,kmeans,machine-learning,o-means,pytorch,reproducible-research,unsupervised-learning,vector-search
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.8
Requires-Dist: faiss-cpu>=1.7.4; python_version < '3.9'
Requires-Dist: faiss-cpu>=1.8; python_version >= '3.9'
Requires-Dist: h5py>=3.10
Requires-Dist: matplotlib>=3.7; python_version < '3.9'
Requires-Dist: matplotlib>=3.8; python_version >= '3.9'
Requires-Dist: numpy>=1.24; python_version < '3.9'
Requires-Dist: numpy>=1.26; python_version >= '3.9'
Requires-Dist: pandas>=2.0; python_version < '3.9'
Requires-Dist: pandas>=2.1; python_version >= '3.9'
Requires-Dist: pyarrow>=14
Requires-Dist: pyyaml>=6
Requires-Dist: scikit-learn>=1.3; python_version < '3.9'
Requires-Dist: scikit-learn>=1.4; python_version >= '3.9'
Requires-Dist: scipy>=1.10; python_version < '3.9'
Requires-Dist: scipy>=1.11; python_version >= '3.9'
Requires-Dist: torch>=2.2
Requires-Dist: typer>=0.12
Requires-Dist: typing-extensions>=4; python_version < '3.9'
Description-Content-Type: text/markdown

# KEO

<p align="center">
  <img src="https://raw.githubusercontent.com/rajveer43/keo/main/assets/logo.svg" alt="KEO logo" width="360">
</p>

<p align="center">
  <a href="https://pypi.org/project/keo/"><img alt="PyPI version" src="https://img.shields.io/pypi/v/keo?logo=pypi&logoColor=white"></a>
  <a href="https://pypi.org/project/keo/"><img alt="Monthly downloads" src="https://img.shields.io/pypi/dm/keo?logo=pypi&logoColor=white"></a>
  <a href="https://pypi.org/project/keo/"><img alt="Python versions" src="https://img.shields.io/pypi/pyversions/keo?logo=python&logoColor=white"></a>
  <a href="https://github.com/rajveer43/keo/actions/workflows/ci.yml"><img alt="Tests" src="https://github.com/rajveer43/keo/actions/workflows/ci.yml/badge.svg"></a>
  <a href="https://github.com/rajveer43/keo/blob/main/LICENSE"><img alt="License: MIT" src="https://img.shields.io/pypi/l/keo"></a>
  <a href="https://doi.org/10.5281/zenodo.24273473"><img alt="DOI" src="https://zenodo.org/badge/DOI/10.5281/zenodo.24273473.svg"></a>
  <a href="https://rajveer43.github.io/keo/"><img alt="Website" src="https://img.shields.io/badge/website-rajveer43.github.io%2Fkeo-1a5fb4?logo=githubpages&logoColor=white"></a>
  <img alt="Developed on Apple Silicon" src="https://img.shields.io/badge/developed%20on-Apple%20Silicon-lightgrey?logo=apple&logoColor=white">
</p>

**A reproducible evaluation of k-means, e-means, and o-means on independent datasets.**

**Website and blog: <https://rajveer43.github.io/keo/>**. Preliminary results on four datasets, plus the post *Rethinking K-Means: How e-Means and o-Means Reduce Clustering Computation*.

KEO is an experimental benchmarking framework for evaluating three large-scale clustering algorithms under a unified experimental protocol. It provides consistent implementations, evaluation metrics, experiment tracking, and result generation to investigate whether the performance improvements reported by recent e-means and o-means research generalize to datasets outside their original evaluations.

The goal is not to establish another leaderboard, but to conduct a controlled and reproducible investigation of algorithmic performance, computational cost, and practical limitations.

## Research motivation

Recent work has proposed improvements to traditional k-means clustering:

- **k-means:** The classical Lloyd algorithm, serving as the baseline.
- **e-means:** A mini-batch approach using exponential moving average updates and adaptive optimization strategies, reported to achieve comparable clustering quality in substantially fewer epochs.
- **o-means:** An extension of e-means that uses an inverted-file index over centroids to reduce assignment costs as the number of clusters increases.

KEO evaluates these approaches using the same datasets, initialization, evaluation metrics, and experiment-tracking infrastructure.

| Algorithm | Approach | Reference |
|---|---|---|
| **K — k-means** | Lloyd's algorithm with exact assignment and centroid updates. | Lloyd (1982) |
| **E — e-means** | Mini-batch k-means with EMA updates, cosine step scheduling, scaling laws, and dead-cluster revival. | Bessa et al. (2026) |
| **O — o-means** | e-means with a two-level inverted-file index over centroids to reduce assignment comparisons. | Bajaj, Nupur, Garg (2026) |

## Research question

The central research question is:

**Do the reported improvements of e-means and o-means generalize to datasets that were not used to develop or tune these algorithms?**

Specifically, KEO investigates:

- Whether e-means can achieve k-means-level clustering quality in approximately 1.4–2.6 epochs.
- Whether o-means reduces assignment costs as the number of clusters increases.
- How clustering quality, computational cost, and cluster stability change across experimental configurations.
- Under what conditions these algorithms fail to deliver their expected improvements.

## Datasets

KEO evaluates four datasets from [ANN-Benchmarks](https://ann-benchmarks.com/). None of them appears in the evaluation dataset lists of the e-means or o-means papers, based on the papers and project materials examined for this study.

| Dataset | Config name | Vectors (after cleaning) | Dimensions | Evaluation queries (val / test) | Preprocessing | Source file |
|---|---|---|---|---|---|---|
| GloVe-100 | `glove100` | 1,183,514 | 100 | 5,000 / 5,000 | L2 normalization | `glove-100-angular` |
| NYTimes-256 | `nytimes256` | 289,761 | 256 | 3,794 / 3,794 | L2 normalization | `nytimes-256-angular` |
| Fashion-MNIST | `fashionmnist784` | 60,000 | 784 | 5,000 / 5,000 | none (Euclidean) | `fashion-mnist-784-euclidean` |
| GIST-960 | `gist960` | 1,000,000 | 960 | 492 / 491 | none (Euclidean) | `gist-960-euclidean` |

Notes:

- GloVe-100 is the primary dataset and was run first, at three values of k (4,096 / 16,384 / 32,768). The other three were run afterwards with the same protocol: NYTimes-256 at k = 4,096 and 16,384, Fashion-MNIST at k = 2,048 and GIST-960 at k = 4,096.
- Held-out queries are deduplicated against the training set, then split into validation and test halves with a fixed seed. NYTimes-256 lost 239 all-zero training vectors and 2,384 of its 10,000 queries this way; GIST-960 has only 1,000 queries, so its test set is small.
- Last.fm (`lastfm-64-dot`) was considered and dropped: its ANN-Benchmarks file has a padding column that turns dot-product search into a Euclidean problem, so L2 k-means would measure the padding.
- Each dataset is downloaded automatically and verified using a SHA-256 checksum recorded on the first download (ANN-Benchmarks publishes none).
- Particle-physics and other tabular datasets (CSV, Parquet, plain HDF5 arrays) can be converted with `python scripts/convert_physics_dataset.py SOURCE --out data/<name>/<name>.hdf5`. The converter does a seeded train/test split and pads or truncates variable-length jets to a fixed width. A dataset YAML with `source.type: local` and `standardize: true` then z-scores each feature using the training rows only. These are feature vectors, not embeddings, so their results are reported separately (`docs/ASSUMPTIONS.md` A59, A60). No physics dataset has been run yet.

## Installation

### Requirements

- Python 3.8 to 3.14 are supported (CI runs the tests on every one of them)
- Python 3.12 is recommended for the development environment
- Python 3.8 installs older library versions (for example numpy 1.24, scikit-learn 1.3, pandas 2.0), so numerical results may differ slightly in the last digits from newer Pythons. The provenance record stored with each run lists the library versions used.
- The adapter for the official faiss E-means (`faiss.contrib.e_means`) needs faiss-cpu 1.16 or newer, which itself requires Python 3.10 or newer. On Python 3.8 and 3.9 that adapter reports itself as unavailable; the reference implementations and the faiss Lloyd adapter work as usual.

Install KEO using pip:

```bash
pip install keo
```

### Development installation

Clone the repository and install dependencies using [uv](https://docs.astral.sh/uv/):

```bash
git clone https://github.com/rajveer43/keo.git
cd keo

uv sync
```

## Quick start

Run your first experiment:

```bash
# Download the GloVe-100 dataset (~460 MB)
keo download glove100

# The other datasets: nytimes256, fashionmnist784, gist960 (GIST alone is 3.8 GB)
keo list-datasets

# Run an end-to-end smoke test
keo smoke

# Generate figures from the smoke experiment
keo figures smoke

# Check available official implementations
keo adapters
```

The smoke experiment runs on a small subset of the dataset and takes approximately 10 seconds.

### Running experiments

Preview an experiment before execution:

```bash
keo run <name-or-path.yaml> --dry-run
```

Execute the experiment:

```bash
keo run <name-or-path.yaml>
```

Inspect experiment progress:

```bash
keo status <name>
```

Generate figures:

```bash
keo figures <name> --theme both
```

From a source checkout, prefix commands with `uv run`. For example:

```bash
uv run keo smoke
uv run pytest
```

### Data and results

KEO uses configurable directories for datasets, results, and experiment configurations.

| Environment variable | Purpose |
|---|---|
| `KEO_DATA_DIR` | Dataset storage |
| `KEO_RESULTS_DIR` | Experiment results |
| `KEO_CONFIGS_DIR` | Experiment configurations |

By default:

- Source installations use `data/` and `results/` within the repository.
- Installed packages cache datasets in `~/.cache/keo/data`.
- Results are written to `./results` relative to the current working directory.

## Methodology

### Implementation strategy

Each algorithm is evaluated through two separate implementations.

**1. Reference implementation**

Our own PyTorch CPU implementations, developed from the corresponding research papers.

**2. Official implementation**

The authors' released implementations, accessed through isolated worker processes and adapters, including Faiss `contrib/e_means`.

These implementations are never mixed within an experiment. Every result records its implementation provenance.

Clustering quality is compared across implementations. Wall-clock execution time is compared only within the same implementation to avoid misleading performance comparisons.

### Evaluation metrics

KEO tracks the following metrics:

| Metric | Description |
|---|---|
| MSE | Mean squared L2 distance to the assigned centroid, evaluated on training and held-out points. |
| Relative MSE (Gap) | Error relative to k-means run for 25 iterations from the same initialization and random seed. |
| Cluster health | Dead clusters, empty clusters, and Faiss imbalance factor. |
| Computational cost | Distance computations for assignment and index maintenance. |
| Wall-clock time | Fitting time, compared within the same implementation. |

MSE is computed using exact assignment in float64 precision on both training and held-out points.

Computational cost is reported in two forms:

- Actual distance computations performed.
- Nominal cell arithmetic.

This provides a hardware-independent perspective on algorithmic complexity.

### e-means configurations

KEO supports two e-means recipes:

- `official`: Uses the constants provided by the official implementation. This is the default configuration.
- `paper`: Uses the values specified in the research paper.

The two configurations differ slightly. All headline evaluations use the official recipe.

### Reproducibility and experiment tracking

Reproducibility is a central design principle of KEO.

Each experiment result is associated with a content hash derived from its configuration.

This provides several benefits:

- Experiments can resume after interruption.
- Previously completed result cells are not recomputed.
- Configuration provenance is preserved.
- Results remain traceable to their corresponding experimental settings.

### Figures and reporting

KEO generates six figures directly from stored experiment results.

Each figure includes:

- A corresponding CSV containing the exact plotted values.
- Light and dark theme variants.
- Data-driven headline calculations.

No headline metrics are manually entered into the figures.

## Project status

KEO is under active development. The experimental infrastructure and core algorithm implementations are complete, while the main benchmark and analysis phases remain in progress.

| Phase | Scope | Status |
|---|---|---|
| 0–2 | Specification, dataset pipeline, metrics, k-means | Complete |
| 3–4 | e-means and o-means reference implementations | Complete |
| 5 | Official implementation adapters | Complete |
| 6 | Result storage, resumable execution, figure generation | Complete |
| 7 | Full benchmark grid | In progress: GloVe-100 at full size (k = 4,096 / 16,384 / 32,768, 3 seeds, 252 cells) and NYTimes-256, Fashion-MNIST and GIST-960 at full size (3 seeds, 336 cells) are complete; GloVe-100 subsets (n = 100k to 1M) and seeds 3–9 not run |
| 8 | Analysis and report generation | Not started |
| 9 | Ablations, scaling-law transfer, secondary experiments | Not started |
| 10 | Hardening and packaging | Not started |

### Current validation

- 179 automated tests passing on the development machine.
- Smoke experiment covering 26 experimental cells.
- Successful end-to-end execution of the smoke pipeline.
- GitHub Actions CI badge tracks the latest Linux test run.

### Implementation agreement

On GloVe subsets, the reference e-means implementation matches the official implementation within approximately 0.2% on held-out error.

In the full runs, the two agree within 0.26% on GloVe-100 and within 0.70% on the other datasets at integer pass budgets. At fractional budgets (1.5 and 12.8 passes) they differ by up to 1.95%, so the 0.5% parity target is not met there on GloVe-100, NYTimes-256 (k = 16,384) or Fashion-MNIST. The suspected cause is a difference in the fractional-pass schedule, plus the remainder rows the reference drops from every epoch, but this has not been isolated by an experiment.

### Preliminary results

Median over 3 seeds, GloVe-100, NYTimes-256, Fashion-MNIST and GIST-960, Apple M4 CPU:

- e-means reaches the quality of 25 Lloyd iterations in about 1.4 to 3.2 passes over the data. The number needed depends on the dataset (NYTimes-256 at k = 16,384 needed about 3.1 to 3.2).
- o-means (our reconstruction) needs far fewer distance computations on GloVe-100 at larger k (0.26 full scans at k = 16,384 and 0.14 at k = 32,768, against 1.90 and 1.77 for e-means), a moderate amount on Fashion-MNIST and GIST-960, and little on NYTimes-256 (2.24 against 3.04 scans at k = 16,384). The reason for the NYTimes-256 result is not yet known.

The full table and discussion are in the [blog post](https://rajveer43.github.io/keo/posts/glove-e-means-o-means/) (section 11) and in `results/cross_dataset/tables/match_lloyd25.csv`.

**Important: these are preliminary results, not benchmark conclusions.** There are three seeds per configuration, no significance tests, one machine, and the o-means numbers are for our reconstruction. Dataset size, dimension and k differ between datasets, so a difference between datasets cannot be put down to the data alone.

Results in `results/smoke/` are based on two repetitions at `k = 512`. They validate the experimental pipeline and should not be interpreted as answers to the research question.

## Limitations

The current evaluation has several limitations that should be considered when interpreting its results.

**1. o-means reconstruction**

The o-means paper has not been released. Its implementation is reconstructed using the available project page, with unspecified details documented as explicit assumptions. Conclusions about o-means therefore apply to this reconstruction.

**2. Hardware limitations**

The reference development environment uses an Apple M4 CPU without GPU execution for the reference implementations. GPU performance claims from the original papers cannot currently be reproduced in this environment.

Clustering quality and computational operation counts can still be evaluated.

**3. CPU timing limitations**

Index maintenance in o-means may have different performance characteristics on GPUs and CPUs. Consequently, CPU wall-clock measurements should not be treated as a definitive assessment of the algorithm's performance.

**4. Dataset coverage**

The current evaluation uses four ANN-Benchmarks datasets (GloVe-100, NYTimes-256, Fashion-MNIST, GIST-960), all image or text-derived vectors. Results may not generalize to other kinds of data, such as sentence or multimodal embeddings or particle-physics features. Evaluating further datasets is tracked in the [issue tracker](https://github.com/rajveer43/keo/issues/14).

**5. Dataset novelty**

The claim that these datasets are independent is bounded to the dataset lists examined in the two papers. It is not a claim that they have never been used in previous clustering research. GIST-960 is a different dataset from the SIFT descriptors used in the e-means paper, but both are image descriptors.

## Repository structure

```text
keo/
├── configs/              # Dataset and experiment configurations
├── src/
│   └── keo/
│       ├── algos/        # k-means, e-means, o-means
│       │   └── adapters/ # Official implementation adapters
│       ├── bench/        # Experiment runner and result storage
│       ├── metrics/      # Evaluation metrics and operation counting
│       ├── analysis/     # Figure generation and analysis
│       └── data/         # Download, verification, preparation
├── scripts/              # Benchmark runners and the cross-dataset table
├── tests/                # Unit and phase-gate tests
├── results/              # Experiment outputs
├── blog/                 # Hugo site (landing page and blog post)
├── CONTRIBUTING.md
├── CODE_OF_CONDUCT.md
├── SECURITY.md
├── CHANGELOG.md
└── LICENSE
```

## References

### Clustering algorithms

- **e-means:** Bessa et al. (2026). *e-means: Outperforming k-means (in one-tenth the epochs).* [Paper](https://arxiv.org/abs/2611.09501) · [Project page](https://e-means.github.io)
- **o-means:** Bajaj, Nupur, Garg (2026). *o-means: Index the centroids, don't scan them.* Project page, November 2026. Paper not yet released.
- **Flash-KMeans:** GPU k-means baseline. [arXiv:2603.09229](https://arxiv.org/abs/2603.09229)
- **Faiss:** Douze et al. (2024). [arXiv:2401.08281](https://arxiv.org/abs/2401.08281)

### Related research

- **VQ-VAE:** van den Oord et al. (2017). [arXiv:1711.00937](https://arxiv.org/abs/1711.00937)
- **ANN-Benchmarks:** Aumüller et al. (2018). [arXiv:1807.05614](https://arxiv.org/abs/1807.05614)
- **GloVe:** Pennington et al. [Project page](https://nlp.stanford.edu/projects/glove/)
- **VIBE:** [arXiv:2505.17810](https://arxiv.org/abs/2505.17810)

### Classical foundations

- Lloyd (1982). *Least squares quantization in PCM.*
- Arthur and Vassilvitskii (2007). *k-means++: The advantages of careful seeding.*
- Sculley (2010). *Web-scale k-means clustering.*

## Contributing

Contributions are welcome, and you do not need to write code to help. Running the benchmark on a GPU, suggesting a dataset, or reviewing the o-means reconstruction are all useful. See [CONTRIBUTING.md](CONTRIBUTING.md) for how to set up, what a good pull request looks like, and the rules that keep the benchmark trustworthy. Please follow the [Code of Conduct](CODE_OF_CONDUCT.md). To report a security problem, see [SECURITY.md](SECURITY.md).

If you use KEO in your work, please cite it using [CITATION.cff](CITATION.cff) or the DOI badge above.

## License

KEO is released under the [MIT License](https://github.com/rajveer43/keo/blob/main/LICENSE).

The datasets are not redistributed with the package. They are downloaded from ANN-Benchmarks and remain subject to their respective terms.

---

<p align="center">
  <b>KEO — Reproducible clustering research.</b>
  <br>
  Built for transparent, controlled, and reproducible evaluation.
</p>