Skip to content

A k-nearest neighbors algorithm is implemented in Python from scratch to perform a classification or regression analysis.

Notifications You must be signed in to change notification settings

DavidCico/Self-implementation-of-KNN-algorithm

Repository files navigation

Self-implementation-of-KNN-algorithm

The current repository contains different scripts, in which functions are implemented in Python from scratch, to carry out a classification or regression problem using a k-nearest neighbors (KNN) algorithm.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

You need Python 3.x to run the following code. You can have multiple Python versions (2.x and 3.x) installed on the same system without problems.

In Ubuntu, Mint and Debian you can install Python 3 like this:

sudo apt-get install python3 python3-pip

The Jupyter Notebook which can be installed through Python's package manager:

pip3 install --upgrade pip
pip3 install jupyter

For other Linux flavors, OS X and Windows:

Python is available at http://www.python.org/getit/
Jupyter Notebook at https://jupyter.readthedocs.io/en/latest/install.html

File descriptions

  • The files "iris.csv" and "abalone.csv" correspond to the datasets used in the examples.
  • "Open_conversion_data.py" contains all initial operations to be done to read the data from a CSV file, modify the variable types in the different columns (features) of the dataset, as well as data re-scaling, such as normalization and standardization. For more information on the different operations, the reader is referred to the Jupyter notebook Open_conversion_data.ipynb where the different functions are more elaborated.

  • In "Split_dataset.py", 2 approaches to split a dataset are implemented, to understand how the split between training and testing occur for machine learning problems. The Jupyter notebook Split_dataset.ipynb is available for more details.

  • "Performance_assessment.py" is a Python script in which accuracy metrics to measure machine learning algorithms performance are introduced, and implemented in a simple way. The Jupyter notebook Performance_assessment.ipynb is available for more details.

  • "My_KNN.py" is the Python implementation of the k-nearest neighbors model for classification or regression problem.

  • "DistanceType.py" is the Python script, in which the different distances, that can be used in the KNN algorithm, are defined.

  • "Iris_data_study_classification.py" is the main Python script, calling the different functions from the scripts above, to perform a classification analysis on the Iris flower dataset, with the implemented KNN model.

  • "Iris_data_study_classification.ipynb" is the Jupyter notebook version of the ".py" script.

  • "Abalone_case_study_regression.py" is the main Python script, calling the different functions from the scripts above, to perform a regression analysis on the UCI Abalone dataset, with the implemented KNN model. In this dataset, we are trying to predict the age of the abalones based on their physical features.

  • "Abalone_case_study_regression.ipynb" is the Jupyter notebook version of the ".py" script.

Running the files

The different ".py" files need to be placed in the same folder for the main scripts to be run. The code is then ready to be used on the Iris dataset for instance, and just requires running the following command:

python Iris_data_study_classification.py

The notebooks can be directly opened on GitHub. An alternative way to open the notebooks, is to run the Jupyter Notebook. This can be done by executing the following command at the Terminal (Mac/Linux) or Command Prompt (Windows):

jupyter notebook

This will print some information about the notebook server in your terminal, including the URL of the web application (by default, http://localhost:8888):

$ jupyter notebook
[I 11:47:00.830 NotebookApp] Serving notebooks from local directory: C:\Users\EC-PM-3
[I 11:47:00.830 NotebookApp] The Jupyter Notebook is running at:
[I 11:47:00.830 NotebookApp] http://localhost:8888/?token=d22181d47f4826316a37161bb8c8469d77a5851bf9ab2c1f
[I 11:47:00.830 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

It will then open your default web browser to this URL.

When the notebook opens in your browser, you will see the Notebook Dashboard, which will show a list of the notebooks, files, and subdirectories in the directory where the notebook server was started. The notebook can then be chosen by navigating in the Notebook Dashboard.

For more information on how to run a specific jupyter notebook, you can go to the following link.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

Releases

No releases published

Packages

No packages published

Languages