Skip to main content

FAISS implementation of multiclass and multilabel K-Nearest Neighbors Classifiers.

Project description

faissknn

DOI

faissknn contains implementations for both multiclass and multilabel K-Nearest Neighbors Classifier implementations. The classifiers follow the scikit-learn: fit, predict, and predict_proba methods.

Install

faissknn needs a FAISS backend, which you choose at install time via an extra. Pick exactly one — they ship the same faiss module and can't coexist. For CPU (works everywhere — macOS, Windows, Linux; no GPU, CUDA driver, or system toolkit required):

pip install "faissknn[cpu]"

This pulls in faiss-cpu along with numpy and torch.

A bare pip install faissknn installs no FAISS backend and raises a clear error at import time telling you to pick an extra. Always install one of [cpu] or [cuda].

GPU acceleration

On Linux x86_64 with an NVIDIA driver (R525+), use the [cuda] extra, which installs faiss-cuda (Taylor Geospatial's GPU wheels, CUDA 12.8, arch sm_70–sm_120: V100 through B200/RTX-50) instead of faiss-cpu. No system CUDA toolkit needed — the runtime libraries come from nvidia-cuda-runtime-cu12 / nvidia-cublas-cu12 on PyPI. The GPU wheel contains the full CPU implementation too, so it also works on GPU-less machines.

pip install "faissknn[cuda]"

uv/pip can't auto-detect the host CUDA driver, so the backend is a manual choice. Because nothing is installed until you pick an extra, a fresh install of any single extra is clean — no base faiss-cpu to fight, no uninstall/reinstall dance. If you later want to switch backends in the same environment, uninstall the current one first (e.g. pip uninstall -y faiss-cpu) before installing the other extra, since the FAISS packages share the faiss module and can't coexist.

Usage

Multiclass:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

from faissknn import FaissKNNClassifier

x, y = make_classification()
x_train, x_test, y_train, y_test = train_test_split(x, y)
model = FaissKNNClassifier(
    n_neighbors=5,
    n_classes=None,
    device="cpu"
)
model.fit(x_train, y_train)

y_pred = model.predict(x_test) # (N,)
y_proba = model.predict_proba(x_test) # (N, C)

Multilabel:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

from faissknn import FaissKNNMultilabelClassifier

x, y = make_multilabel_classification()
x_train, x_test, y_train, y_test = train_test_split(x, y)
model = FaissKNNMultilabelClassifier(
    n_neighbors=5,
    device="cpu"
)
model.fit(x_train, y_train)

y_pred = model.predict(x_test) # (N, C)
y_proba = model.predict_proba(x_test) # (N, C)

GPU/CUDA: faissknn also supports running on the GPU to speed up computation. Simply change the device to cuda or a specific cuda device cuda:0

model = FaissKNNClassifier(
    n_neighbors=5,
    device="cuda"
)
model = FaissKNNClassifier(
    n_neighbors=5,
    device="cuda:0"
)

Cite

If you use faissknn in your research, please considering citing!

@software{isaac_corley_2026_18370748,
  author       = {Isaac Corley},
  title        = {isaaccorley/faissknn: Zenodo Cite},
  month        = jan,
  year         = 2026,
  publisher    = {Zenodo},
  version      = {v0.0.3},
  doi          = {10.5281/zenodo.18370748},
  url          = {https://doi.org/10.5281/zenodo.18370748},
}

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

faissknn-0.4.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

faissknn-0.4.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file faissknn-0.4.0.tar.gz.

File metadata

  • Download URL: faissknn-0.4.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for faissknn-0.4.0.tar.gz
Algorithm Hash digest
SHA256 52fed6164004952bf7865a358059b125e916e7e8f67c1c7dfb3745932b27f8cd
MD5 6fcde08addec508b9b65238f884ff503
BLAKE2b-256 255ad59a520c655328e1bb4223b8d730f7a6663300d8af283c5b12cef861a2bf

See more details on using hashes here.

File details

Details for the file faissknn-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: faissknn-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for faissknn-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a7bca36c40d3bd6460d471426d0e2422077590fdf447903da446062cef28a6d
MD5 c61edf40befe22e1ac6ee3c1ec232303
BLAKE2b-256 5f5ca24089a4945348dd2037f7841484ac0d3a03adbc4ebf5bb220c3f90504b4

See more details on using hashes here.

Supported by

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