Skip to main content

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

Project description

FAISSKNN

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

The FAISS authors recommend to install faiss through conda e.g. conda install -c pytorch faiss-gpu. See FAISS install page for more info.

Once faiss is installed, faissknn can be installed through pypi:

pip install faissknn

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"
)

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.0.2.tar.gz (4.2 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.0.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: faissknn-0.0.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.5

File hashes

Hashes for faissknn-0.0.2.tar.gz
Algorithm Hash digest
SHA256 382360d1932e4477d627a6f1a9b93394975d967990616bd1feb84f2190322f52
MD5 2e869da72a55542324a3c7c8cb656b03
BLAKE2b-256 5e75a2b849581437f888633f624edd8a4239fd90ffa825b09119e20dde2136c2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: faissknn-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.5

File hashes

Hashes for faissknn-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7738f2d8a26b7fb6e60195fdb58ed70d0c342292d9941fe5e22403c4fd008a57
MD5 c681606c0aeebebc57a60236f71ea872
BLAKE2b-256 e79f28cbd32c51e3f5769c1773cddf17d4bacc0fe2bd56226fdf4fbbbb1e8254

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