Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/isaaccorley/faissknn
Faiss Multilabel and Multiclass KNN Classifier Implementations
https://github.com/isaaccorley/faissknn
Last synced: about 1 month ago
JSON representation
Faiss Multilabel and Multiclass KNN Classifier Implementations
- Host: GitHub
- URL: https://github.com/isaaccorley/faissknn
- Owner: isaaccorley
- License: mit
- Created: 2023-05-22T21:03:25.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-31T04:34:04.000Z (11 months ago)
- Last Synced: 2024-11-22T06:33:44.604Z (about 1 month ago)
- Language: Python
- Size: 20.5 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://github.com/facebookresearch/faiss/blob/main/INSTALL.md) for more info.
Once `faiss` is installed, `faissknn` can be installed through pypi:
```
pip install faissknn
```### Usage
Multiclass:
```python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_splitfrom 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:
```python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_splitfrom faissknn import FaissKNNMultilabelClassifier
x, y = make_multilabel_classification()
x_train, x_test, y_train, y_test = train_test_split(x, y)
model = FaissKNNClassifier(
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`
```python
model = FaissKNNClassifier(
n_neighbors=5,
device="cuda"
)
model = FaissKNNClassifier(
n_neighbors=5,
device="cuda:0"
)
```