Skip to main content

A k-nearest neighbors classifier in Python 3.11.7

Project description

KNN Classifier

A k-Nearest Neighbors supervised classifier in Python 3.11.7


Importing the package

pip install knnclassifier
from knn_classifier 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.

[!NOTE] Ensure that the test data has the same number of fields (columns) as the training data. This is required for the algorithm to function correctly.

  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.

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

knnclassifier-0.0.1.tar.gz (77.0 kB view details)

Uploaded Source

Built Distribution

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

knnclassifier-0.0.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file knnclassifier-0.0.1.tar.gz.

File metadata

  • Download URL: knnclassifier-0.0.1.tar.gz
  • Upload date:
  • Size: 77.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for knnclassifier-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d961571933fb1b20a83ba267f745f6156082fa15d10e2c368406eb13aea5f191
MD5 a92dec602238cfb0f5b74e96bf506352
BLAKE2b-256 8a1949c017fb9fb3c69859bd0e6aec2b1e02d31b41832a449010d1ec41917726

See more details on using hashes here.

File details

Details for the file knnclassifier-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: knnclassifier-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for knnclassifier-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 285e24479ee6d1f081053137b51fb458e88f1e05daef3a97dffbce285e5f41eb
MD5 180eec38d8c92ba3086e8201a5d69d78
BLAKE2b-256 1dd6c8f171f7e8774b1bd60041841dcd70b331b7d5f5abb3e201103d6b93cc19

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