K-Nearest Neighbor Classifier — Implement Homemade Class & Compare with Sklearn Import

What is K-Nearest Neighbors?

Courtesy of Datacamp

K-Nearest Neighbors is an algorithm used in classification and regression problems; however, this example application uses the algorithm as a classifier. K-nn is a form supervised learning where classification outputs are class membership and is considered lazy learning. This object in question is assigned a class based on the classes of its neighbors. This can be modified by alternating the number of neighbors provided (K). In regression the outputs are the property values for the specified objects. The value is the average of the values of k nearest neighbors.

~Objective~

The goal here was to create a model that takes in row values, computes the Euclidean distance between one another and output a prediction of the proper class. Then the results were compared to the sklearn model that can be imported with the Scikit-Learn library.

~Implementation~

The class was implemented to fit the data, predict on the data, and display the data. There is a class definition that calculates the Euclidean distance used for the math stuff. Next the input data is fit into an X_train and y_train (target). Then doing a bunch of fancy math, calculating distances between points, class predictions, analyzing neighbors, and more; the model is able to predict a class. Lastly, a quick function was created to be able to display the data similar to the Sklearn model such that comparison can be easy for the three datasets tested on. These datasets are Iris, Breast Cancer, and Wine datasets from Sklearn. The results and comparison between the imported model and my model are shown below for the listed datasets:

Iris Dataset Comparison
Breast Cancer Dataset Comparison
Wine Dataset Comparison

Results indicate my model performs as well if not better in most rudimentary basics. Please feel free to take this code to the next level and see what you can make of it! Code can be found for the model, testing, and blog are on Github. The class implementation can be seen below:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store