Implementation of k Nearest Neighbor from scratch



Implementing a machine learning algorithm on your own can give better understanding about how the thing works under the hood. This also helps in understanding the mathematical implementations and gives a better overview of the application of different concepts.

In this post I will be talking about implementation of k Nearest Neighbor classifier which is one of the simplest but very effective algorithm in Machine Learning. kNN can classify a new point by examining the class of its nearest neighbors. eg: If there is an area where majority of people residing are from different economic classes like high income , middle income and low income group. So given a new data point this algorithm can classify the person lies in which of the income group based on its nearest neighbors. Under the hood this algorithm uses distance metrics which can be euclidean distance , cosine similarity , taxi cab or Manhattan distance etc.

Below is the link , any feed back is appreciated.


Hi @deepnarainsingh

I just had a quick look look pretty good, if it is for beginners you should work on the read me and to explain or to give reference to the Knn algorithm, for the next steps you should mentioned other algorithm and the concept of distance.
You implemented cosine for example explain it or give a link

Have a good day



Hi Lesaffrea,

Thanks a lot for your valuable feedback. My intention was to write for the mid level practitioners who knows about k-means. But I think you have raised important points that I should be explaining some of the points which can be helpful for the beginners. Also if you can suggest what should be the better way to explain some concepts whether it should be in ipython notebook or in readme file.
I intend to add more algorithms in that .

once again thanks a lot for your inputs.


there another post that I found on the same topic