Skip to main content

A k-Nearest Neighbors (KNN) classifier for Python.

Project description

pkg_pyknnclassifier

Logo

Documentation Status ci-cd codecov

📄 About

Our package, named "pkg_pyknnclassifier," is a comprehensive toolkit for k-Nearest Neighbors (k-NN) modeling and evaluation. It offers a set of functions designed to facilitate various aspects of working with k-NN algorithms, from loading the data, calculating distances to making predictions and assessing model performance. We aim to simplify the process by providing essential functionalities for data manipulation, model evaluation, and scaling.

Our documentaion: (https://pkg-pyknnclassifier.readthedocs.io/en/latest/)

📦 Functions

This package consists of six functions and explained as below:

  • data_loading(str_of_path, target_column): This function loads data from a file path and split into features and target.
  • scaling(df, impute_strategy, scale_method): This function allows user to choose the method of data imputation and scaling, and apply to the data.
  • calculate_distance(obs_1, obs_2, method = "Euclidean"): This function calculates the Euclidean distance between two observations for the KNN model to find the similarity score.
  • find_neighbors(labeled_arraies, unlabeled_array, k): This function finds the indices of the 'k' nearest neighbors in a collection of labeled arrays to a given unlabeled array.
  • predict(train_X, train_y, unlabel_df, pred_method, k): This function predicts the labels of the unlabled observations based on the similarity score calculated from Euclidean distance.
  • evaluate(y_true, y_pred, metric='accuracy'): This function calculates evaluation metrics such as accuracy, precision, recall, and F1 score for a k-NN model based on true labels and predicted labels.

🛠️ Installation

Option 1 (For Users)

The package has been published to PYPI, we could use pip install

  1. Create and activate a virtual environment using conda
$ conda create --name <env_name> pip -y
$ conda activate <env_name>
  1. Install the package using the command below
$ pip install pkg_pyknnclassifier

Option 2 (For Developers)

To sucessfully run the following commands of installation, we would need conda and poetry, guide included in the link (conda, poetry)

  1. Clone this repository
$ git clone git@github.com:UBC-MDS/Group16_knnmodel.git
  1. Direct to the root of this repository
  2. Create a virtual environment in Conda with Python by the following commands at terminal and activate it:
$ conda create --name pyknnclassifier python=3.11 -y
$ conda activate pyknnclassifier
  1. Install this package via poetry, run the following command.
$ poetry install

✅ Testing

To test this package, please run the following command from the root directory of the repository:

$ pytest tests/
  • branch coverage could be viewed with the following command:
$ pytest --cov-branch --cov=pkg_pyknnclassifier

Usage

To successfully use our knn model to predict the target, please first ensure you have followed the instruction of installation, and then run the following line in a python notebook.

from pkg_pyknnclassifier.data_loading import data_loading
from pkg_pyknnclassifier.scaling import scaling
from pkg_pyknnclassifier.predict import predict
from pkg_pyknnclassifier.evaluate import evaluate

features, target = data_loading('data/toy_dataset.csv', 'Target')
X_scaled = scaling(features, 'median', 'StandardScaler')
pred = predict(X_scaled, target, X_scaled, 'hard', 3)
accuracy_result = evaluate(target, pred, metric='accuracy')
print("Accuracy:", accuracy_result)

📚 Package Integration within the Python Ecosystem

pkg_pyknnclassifier, while acknowledging the robustness and the capabilities of scikit-learn's KNeighborsClassifier, aims to offer a specialized and streamlined toolkit tailored explicitly for k-Nearest Neighbors classification tasks. As a lightweight and focused alternative, pkg_pyknnclassifier serves users who seek a concise package that offers calculating distances, making predictions, and evaluating k-NN models functions. While scikit-learn covers a broader spectrum of machine learning algorithms, pkg_pyknnclassifier provides a more specialized package, potentially appealing to those who prefer a tailored implementation of their k-NN workflows.

Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

📜 License

pkg_pyknnclassifier was created by "Bill Wan, Sho Inagaki, Shizhe Zhang, Weiran Zhao". It is licensed under the terms of the MIT license.

📚 Credits

pkg_pyknnclassifier was created with cookiecutter and the py-pkgs-cookiecutter template.

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

pkg_pyknnclassifier-0.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

pkg_pyknnclassifier-0.1.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pkg_pyknnclassifier-0.1.0.tar.gz.

File metadata

  • Download URL: pkg_pyknnclassifier-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for pkg_pyknnclassifier-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fa68b81ea8e35da083f0787bf255af1451ab69ffeeb94cbb76d4b0d9e9cf0ece
MD5 25ccb7d75f3cfde87c7d8ec4d6bf84ea
BLAKE2b-256 767b8a5b534ce388e53e81ecd3e3a549f0b220369ff8acf15e46d7d29bf4578b

See more details on using hashes here.

File details

Details for the file pkg_pyknnclassifier-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pkg_pyknnclassifier-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0123f15173f565dbe1c1523fa3418f44f055d1e6f600774402741d2e40ff23e0
MD5 6191d2da46cd3de628e982adf3a3dbc1
BLAKE2b-256 0b0396d55af7783740113045073066a9344e654283de0a54b08ea2a26622405e

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