Skip to main content

KNN Classifier

A k-Nearest Neighbors supervised classifier in Python 3.11.7


Importing the package

pip install knnclassifier
from knnclassifier import KNNClassifier

This statement imports the KNNClassifier class, which will be used to create your classifier objects.

Using the package

Fitting the classifier

Set up your training and test data and fit the classifier.

  1. Populate two text files with your training and test data.

1.4, 4.0, 15.0, 1.1, 1.6, 5.0
1.2, 4.0, 22.2, 2.2, 3.0, 6.0
...

Each line serves as a record and contains a list of its data points (fields), with the record's label as the final item in the list. In the above examples, the labels are 5.0 and 6.0, respectively. Ensure that fields are separated by a consistent delimiter (e.g., a comma and a space).

  1. Split the data and label arrays from the files.
train_data, train_label = KNNClassifier.split_data_label_from_file("train.txt", delimiter=", ")
test_data, actual_label = KNNClassifier.split_data_label_from_file("test.txt", delimiter=", ")

The static method split_data_label_from_file returns two numpy arrays. The first contains all columns except the last (the data columns), and the second contains the last column (the label column).

You may also specify the delimiter. By default, it is ", ", but you may specify any string.

  1. Initialize a KNNClassifier object and fit it to the training arrays.
classifier = KNNClassifier()
classifier.fit(train_data, train_label)

Making and checking predictions with the classifier

  1. Compute and store label predictions for the test data.
test_label_predictions = classifier.predict(test_data, k=5)

The predict method returns the predicted labels of each record of the test data. Specify the k value to be used during prediction.

  1. Check the accuracy of the label predictions.
accuracy: float = KNNClassifier.check_accuracy(test_label_predictions, actual_label, print_info=False)

The static method check_accuracy returns the proportion of the predicted labels that match the actual labels.

You may also specify print_info (default: False). If set to True, each label will be printed individually.

Release files for knnclassifier 1.0.0

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

Source distribution (sdist)

Source distribution for knnclassifier 1.0.0
File Size Uploaded
knnclassifier-1.0.0.tar.gz 77.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for knnclassifier 1.0.0
File Interpreter ABI Platform
knnclassifier-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 81.5 kB

Release files / knnclassifier-1.0.0.tar.gz

Download URL knnclassifier-1.0.0.tar.gz
Size 77.0 kB
Tags Source
SHA-256 checksum
How to use checksums
517fd4009360b21558bcbc67ec41facc782c91cdcbf5c547f2e02e5fcda7b75e
BLAKE2b-256 checksum
How to use checksums
43efc587e891f825e3a844e730070ca6438d734a5d42802914cc647be40e92f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release files / knnclassifier-1.0.0-py3-none-any.whl

Download URL knnclassifier-1.0.0-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4315850ab8acc53d0f6aa25d287bfe94822a4e4b243a9ce7b484f7205c4d2821
BLAKE2b-256 checksum
How to use checksums
c5f898412d59b68b5e39bc0770aee24a9f0ce4f7231cf1aaad2959de2c11895a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.0.1

2 release files

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