Skip to main content

KNN Engine Core

PyPI version Python Build system Bindings

High-performance K-Nearest Neighbors (KNN) classification engine implemented in C++17, with adaptive Principal Component Analysis (PCA) for dimensionality reduction, and exposed to Python via pybind11.

This project is designed for fast experimentation from Python while keeping the compute-intensive path in native C++.

Highlights

  • C++17 implementation of KNN with efficient top-k neighbor selection (std::nth_element).
  • Adaptive PCA that supports:
    • variance-threshold mode (for example 0.95 keeps enough components to preserve 95% variance),
    • fixed-component mode (for example 28 keeps exactly 28 components).
  • Automatic thin PCA path for high-dimensional datasets where features > samples.
  • Python bindings (knn_core) with a minimal, clean API.
  • Build system based on CMake + scikit-build-core + pybind11.

Repository Structure

.
├── CMakeLists.txt
├── CMakePresets.json
├── pyproject.toml
├── include/
│   ├── KNN.hpp
│   ├── KNNEngine.hpp
│   └── PCA.hpp
├── src/
│   ├── KNN.cpp
│   ├── KNNEngine.cpp
│   ├── PCA.cpp
│   └── binding.cpp
└── examples/
    └── script.py

How It Works

  1. KNNEngine.train(X, y, scale) fits PCA on X and projects training data.
  2. The projected training data is passed to the internal KNN classifier.
  3. During inference, incoming samples are projected using the same PCA transform.
  4. KNN predicts labels by majority vote among the nearest neighbors.

PCA Modes

  • Standard mode: used when number of features <= number of samples.
  • Thin mode: used when number of features > number of samples (memory- and compute-friendly formulation).

Requirements

  • Python 3.8+
  • CMake 3.18+
  • C++17 compiler
  • Ninja (recommended generator)

Python dependencies are declared in pyproject.toml:

  • numpy
  • scikit-learn

Installation

Option 1: Install from PyPI (recommended)

pip install knn-engine-core

This installs the published package and makes knn_core importable.

Quick verification after install:

python -c "import knn_core; cfg = knn_core.KNNConfig(); print('knn_core OK | k =', cfg.k, '| variance =', cfg.variance)"

Option 2: Install from source (editable)

From the project root:

python -m pip install -U pip
python -m pip install -e .

Option 3: Build with CMake presets

cmake --preset Release
cmake --build --preset Release

Artifacts are generated under out/build/Release.

Quick Start

import numpy as np
import knn_core

# Example data
X = np.array([
    [5.1, 3.5, 1.4, 0.2],
    [4.9, 3.0, 1.4, 0.2],
    [6.2, 3.4, 5.4, 2.3],
    [5.9, 3.0, 5.1, 1.8],
], dtype=np.float64)

y = ["setosa", "setosa", "virginica", "virginica"]

cfg = knn_core.KNNConfig()
cfg.k = 3
cfg.variance = 0.95

engine = knn_core.KNNEngine(cfg)
engine.train(X, y, scale=False)

sample = np.array([6.0, 3.1, 5.0, 1.9], dtype=np.float64)
pred = engine.predict(sample)
print(pred)

Python API

KNNConfig

  • k: int (default: 3)
    • Number of neighbors used for voting.
  • variance: float (default: 0.95)
    • If < 1.0: treated as explained-variance threshold.
    • If >= 1.0: treated as fixed number of principal components.

KNNEngine(config: KNNConfig)

  • train(X, y, scale=False)
    • X: 2D numpy.ndarray (float64 recommended), shape (n_samples, n_features)
    • y: list of labels (strings)
    • scale: whether to standardize features before PCA
  • predict(sample)
    • sample: 1D feature vector
    • returns predicted label (str)
  • predict_batch(samples)
    • samples: 2D feature matrix
    • returns list of predicted labels (list[str])

Benchmark Results

The following results were produced using the provided evaluation script pattern (examples/script.py) with train/test split and fixed random seed. Timings compare KNNEngine against an equivalent scikit-learn pipeline (PCA + KNeighborsClassifier).

Dataset Accuracy KNNEngine Time Sklearn Time Speedup
Olivetti Faces 92.50% 0.1447s 0.3633s 2.51×
Handwritten Digits 98.06% 0.0231s 0.1203s 5.20×
Iris Flower 100.00% 0.0004s 0.0108s 26.55×

Console logs:

--- Testing Olivetti Faces ---
[PCA] Fit complete. Mode: Thin | Components: 62
[Engine] Trained successfully. Reduced to 62 dimensions.
KNNEngine  — Accuracy: 92.50%  |  Time: 0.1447s
Sklearn    — Accuracy: 92.50%  |  Time: 0.3633s
Speedup: 2.51x

--- Testing Iris Flower ---
[PCA] Fit complete. Mode: Standard | Components: 3
[Engine] Trained successfully. Reduced to 3 dimensions.
KNNEngine  — Accuracy: 100.00%  |  Time: 0.0004s
Sklearn    — Accuracy: 100.00%  |  Time: 0.0108s
Speedup: 26.55x

--- Testing Handwritten Digits ---
[PCA] Fit complete. Mode: Standard | Components: 28
[Engine] Trained successfully. Reduced to 28 dimensions.
KNNEngine  — Accuracy: 98.06%  |  Time: 0.0231s
Sklearn    — Accuracy: 98.06%  |  Time: 0.1203s
Speedup: 5.20x

Running the Example Script

python examples/script.py

Notes and Limitations

  • Labels are currently represented as strings in the C++ core API.
  • Distance metric is Euclidean (squared Euclidean for ranking).
  • Tie-breaking follows std::map key ordering when vote counts are equal.

Development

Typical local workflow:

python -m pip install -e .
python examples/script.py

For release-style native builds:

cmake --preset Release
cmake --build --preset Release

Metadata

Release files for knn-engine-core 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for knn-engine-core 0.1.4
File Size Uploaded Provenance
knn_engine_core-0.1.4.tar.gz 10.0 kB With provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for knn-engine-core 0.1.4
File
knn_engine_core-0.1.4-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
knn_engine_core-0.1.4-cp314-cp314t-macosx_26_0_arm64.whl CPython 3.14 CPython 3.14 free-threading macOS 26.0+ ARM64 Details
knn_engine_core-0.1.4-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
knn_engine_core-0.1.4-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
knn_engine_core-0.1.4-cp314-cp314-macosx_26_0_arm64.whl CPython 3.14 CPython 3.14 macOS 26.0+ ARM64 Details
knn_engine_core-0.1.4-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
knn_engine_core-0.1.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
knn_engine_core-0.1.4-cp313-cp313-macosx_26_0_arm64.whl CPython 3.13 CPython 3.13 macOS 26.0+ ARM64 Details
knn_engine_core-0.1.4-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
knn_engine_core-0.1.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
knn_engine_core-0.1.4-cp312-cp312-macosx_26_0_arm64.whl CPython 3.12 CPython 3.12 macOS 26.0+ ARM64 Details

Total release size: 4.1 MB

Release files / knn_engine_core-0.1.4.tar.gz

Download URL knn_engine_core-0.1.4.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
21f8cfc11292319168ed46bf17e4dbe9c3604e2aec3dc1113a38f822fefd2b46
BLAKE2b-256 checksum
How to use checksums
c1c76d35db83ebe9452b226a9f3d003a259dda5ec58203cbb0cd79a7749d4d4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL knn_engine_core-0.1.4-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 298.2 kB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
1d47fa422e89056746f09c4ecc55ff5fd05d7f67c795a61d5687af5b4a4dbcdb
BLAKE2b-256 checksum
How to use checksums
b96e5dad9c2a36c0ccc33f69075422e9f324adb011a87516c5c7f2486aa1cde8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp314-cp314t-macosx_26_0_arm64.whl

Download URL knn_engine_core-0.1.4-cp314-cp314t-macosx_26_0_arm64.whl
Size 398.4 kB
Tags CPython 3.14 CPython 3.14 free-threading macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
bdd99e899f2d96f52df734b1ce593bd6c37e823c505bc35467179dc4ea4aaec5
BLAKE2b-256 checksum
How to use checksums
f4269a77d96b9f90041d054f08fd4040eff316f36d943bbe0f846ed5cb1c1824
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp314-cp314-win_amd64.whl

Download URL knn_engine_core-0.1.4-cp314-cp314-win_amd64.whl
Size 461.2 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
59d3a52f16ed37586553f05ea8c6de697d5d7ab27b11e7d29428941c955178de
BLAKE2b-256 checksum
How to use checksums
4cfb961d504a3690598bfeffee900b1e878c99cb7e859b7e9d0866e93555f63a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL knn_engine_core-0.1.4-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 296.6 kB
Tags CPython 3.14 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
47f68283743f8ecc6e9ecd85beb5ffadb06179a36303789832f90c4b48f0c907
BLAKE2b-256 checksum
How to use checksums
cd7b64ab451c7b19ab96ae1277efbbe4b42721a2d3ad8de79e387434fb3b7379
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp314-cp314-macosx_26_0_arm64.whl

Download URL knn_engine_core-0.1.4-cp314-cp314-macosx_26_0_arm64.whl
Size 392.5 kB
Tags CPython 3.14 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
540d5f23c3cde9300792613d6207314f8985c58f504f28cd33c4fc9d16c8524d
BLAKE2b-256 checksum
How to use checksums
e6a881a2031de911daa6e7bfbc65608779b016d0effccd74b03c14d021f06462
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp313-cp313-win_amd64.whl

Download URL knn_engine_core-0.1.4-cp313-cp313-win_amd64.whl
Size 446.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
151f95a508e78fb1b9c648b42f52bcd6e1009c79284c15e688a3e0a3d57ac11a
BLAKE2b-256 checksum
How to use checksums
046b7d46f0f49d5ae87f2b64a5bf9e11fe3d1dab850e65b9d6fbd89e0afd5733
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL knn_engine_core-0.1.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 296.4 kB
Tags CPython 3.13 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
5392aa2a2def0ee3fd5353e2f2d796968ae15ef2a0be2ceda7e917f1d321df8c
BLAKE2b-256 checksum
How to use checksums
a48ac9df016a6446e22c8e947d244905c90bcd02efb69cf56ee1e2fb66d3096e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp313-cp313-macosx_26_0_arm64.whl

Download URL knn_engine_core-0.1.4-cp313-cp313-macosx_26_0_arm64.whl
Size 392.2 kB
Tags CPython 3.13 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
eec8980a382c0234acf51a76a5b47238a8b509ae968fdb9114bf348aecba2325
BLAKE2b-256 checksum
How to use checksums
165a7625f582312d226565d9e7aaa65888b39ec4443e5f609a513247de579c75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp312-cp312-win_amd64.whl

Download URL knn_engine_core-0.1.4-cp312-cp312-win_amd64.whl
Size 446.7 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
3184293265690a3e2838b0475adc0887379d2521114ef13d02753706316b4a9e
BLAKE2b-256 checksum
How to use checksums
a70cbee589fb7e11f904a2505235281047486aa00a4e5422162e609930368abb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL knn_engine_core-0.1.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 296.4 kB
Tags CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
ed00caad2a594de803db68cc583077b3dff5cf6d61ee2655b09a6dd371bbee9b
BLAKE2b-256 checksum
How to use checksums
ad8c8163fa76ac0f7bfec1b18513fa6146a2e00ace3a5e17a234bcc1efbcc8ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Release files / knn_engine_core-0.1.4-cp312-cp312-macosx_26_0_arm64.whl

Download URL knn_engine_core-0.1.4-cp312-cp312-macosx_26_0_arm64.whl
Size 392.2 kB
Tags CPython 3.12 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
b0e5e54cc41e6a4a180c813d295587bd9ff87474bb751c8bfc42ec9ea4b87594
BLAKE2b-256 checksum
How to use checksums
45de062de91157c96e479765564dd0df4f2cb1eadf09a6993bafc18f0b928aff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

Security for knn-engine-core 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 12 files in this release have been uploaded with provenance metadata.

Select from 12 files

Attestation for knn_engine_core-0.1.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page