Skip to content

nk-tech-lab/k-nearest-neighbors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

K-Nearest Neighbors (KNN) Implementation

This repository contains three Jupyter notebooks demonstrating the implementation of the K-Nearest Neighbors (KNN) algorithm on 2D and 3D artificial datasets. The notebooks follow the guide provided by DigitalOcean's community tutorial on KNN in Python.

Notebooks

  1. knn_2d.ipynb
  2. knn_3d.ipynb
  3. knn_no_sklearn.ipynb

Dependencies

The notebooks require the following Python libraries:

  • pandas
  • numpy
  • matplotlib
  • seaborn
  • scikit-learn

You can install the required libraries using pip:

pip install -r requirements.txt

Description

knn_2d.ipynb

This notebook covers:

  • Data Creation: Generate a 2D dataset with 500 samples.
  • Visualization: Scatter plot of the dataset.
  • Data Splitting: Split into training and testing sets (80-20).
  • Model Building: Instantiate KNN models with different neighbor values (1, 5, 10, 100).
  • Evaluation: Train and evaluate model accuracy.

knn_3d.ipynb

This notebook covers:

  • Data Creation: Generate a 3D dataset with 500 samples.
  • Visualization: 3D scatter plot of the dataset.
  • Data Splitting: Split into training and testing sets (80-20).
  • Model Building: Instantiate KNN models with different neighbor values (1, 5, 10, 100).
  • Evaluation: Train and evaluate model accuracy.

knn_no_sklearn.ipynb

This notebook covers:

  • Data Creation: Generate a dataset with 500 samples.
  • Visualization: 2D and 3D scatter plots of the dataset.
  • Data Splitting: Split into training and testing sets (80-20).
  • Custom KNN Implementation: Implement the KNN algorithm from scratch without using scikit-learn.
  • Evaluation: Train and evaluate model accuracy using the custom KNN implementation.

Usage

  1. Clone the repository:

    git clone <repository-url>
  2. Navigate to the directory:

    cd <repository-directory>
  3. Open the Jupyter notebooks:

    jupyter notebook knn_2d.ipynb
    jupyter notebook knn_3d.ipynb
    jupyter notebook knn_no_sklearn.ipynb
  4. Run the cells in each notebook to execute the KNN implementation steps.

References


---

This README provides a concise guide to understanding and running the provided notebooks. Feel free to modify it according to your needs.

About

Simple implementation of k-nearest-neighbor in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published