Skip to main content

A kNN classifier based on NCD distance metric for text compression

Project description

NCD-KNN-Classifier

A kNN classifier based on the Normalized Compression Distance (NCD) for text classification.

Installation

Install the package using pip:

pip install NCD-KNN-Classifier

Usage

Here's a basic example of how to use the classifier:

from datasets import load_dataset
from NCD_KNN_Classifier import CompNCDClassifier

# Example of imdb dataset for classification of two labels
dataset = load_dataset("imdb")
test_samples = dataset["test"].shuffle(seed=42).select(range(200))

# Compressing and Saving the Training Dataset Footprint
classifier = CompNCDClassifier(
    train_dataset=dataset['train'],
    test_dataset=test_samples,
    k=3,
    compressor="gzip",
    verbose=True
)
classifier.save_to_pickle("train_footprint.pkl")

# Inference on the test set
metrics = classifier.evaluate()
print("Evaluation metrics:", metrics)

Output Example

After running the example, you should see output similar to this:

INFO:NCD_KNN_Classifier.classifier:Evaluating on the dataset...
Evaluating 200/200: 100%|██████████| 200/200 [00:55<00:00,  3.63it/s]
Evaluation metrics: {'accuracy': 0.69, 'precision': 0.6969369369369369, 'recall': 0.69, 'f1_score': 0.68476726342711}

New Features in v0.2.1

  • Added explainability in the classify function, providing information about the nearest neighbors and their influence on classification decisions.

  • Introduced the optimizer function to assist in finding the optimal k and compressor type.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

ncd_knn_classifier-0.2.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

ncd_knn_classifier-0.2.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file ncd_knn_classifier-0.2.1.tar.gz.

File metadata

  • Download URL: ncd_knn_classifier-0.2.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.8

File hashes

Hashes for ncd_knn_classifier-0.2.1.tar.gz
Algorithm Hash digest
SHA256 acb67285ca6e421f7f0374fefd11ecb247098dd6cee2bd6ca73772224b4e534d
MD5 e77338f17e0a9683c332625998075e18
BLAKE2b-256 cdd60670d37ef88ebab865408a68e872b19bb9299d329e4a95841ab16d879373

See more details on using hashes here.

File details

Details for the file ncd_knn_classifier-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ncd_knn_classifier-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 33ef94b3cd4f6dfb712e783afc7c593117a9b3c2dbd2133d37f4a84138ec76f7
MD5 bb7306f3dc4201d0b3d1d598e65168b0
BLAKE2b-256 21b8f208ee0fe9e116568def5ac079caf30b00a0b241c9f57a2e5849a74f6493

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