How to build your K-Nearest Neighbors in Python from scratch

Semo Edam
3 min readSep 25, 2020

Introduction to K-NearestNeighbors classifier:

K-nearest neighbor classifier is one of the introductory supervised classifier, it addresses the pattern recognition problems, and also the best choices for addressing some of the classification related tasks.

K-nearest neighbor classifier algorithm was proposed by Fix & Hodges in 1951 for performing pattern classification task.

K-NearestNeighbours Pseudocode:

KNN code using Python:

Euclidean distance:

The euclidean distance is presented as follow, and in my code I will import numpy for basic math functions:

KNN class:

The following is the K-NearestNeighbors class with three methods.

Test the built KNN algorithm on Spotify dataset:

After building the algorithm, I will use Spotify dataset from Kaggle. The goal is to predict whether or not I would like a song, using my KNN then compare the score with built in Sklearn KNN.

Upload the dataset:
You can Download dataset from here

Print the first 5 rows to check how our dataset looks:

Split the dataset to train and test sets:

Run the test with both algorithms:

Before testing the algorithms, the training and testing sets will need to be converted to numpy arrays

Results using the built KNN:

Results using sklearn KNN:

Conclusion:

In conclusion, this was a step by step building K-Nearest Neighbors algorithm by using base python. After testing and comparing the Sklearn KNN and the built algorithm, the same score was given. Although, there is a quite large difference in the run time between the two tests.

Links:
Notebook from here
My Github profile here

--

--