K-Nearest Neighbor from Scratch in Python

Опубликовано: 07 Март 2026
на канале: Deep Learning with Yacine
2,854
48

K-Nearest Neighbor is a simple machine learning algorithm that is intuitive and easy to learn. Watch here how I code one from scratch in Python! 🐍

Table of Content
Introduction: 0:00
Theory: 1:50
Scikit-Learn Implementation: 3:44
Python Implementation: 7:08
Conclusion: 11:24

Github link: https://github.com/yacineMahdid/artif...

KNN is a very simple algorithm for classification, wikipedia define it as so:

"In k-NN classification, the output is a class membership. An object is classified by a plurality vote of its neighbors, with the object being assigned to the class most common among its k nearest neighbors (k is a positive integer, typically small). If k = 1, then the object is simply assigned to the class of that single nearest neighbor."


Which means that the label of a given point is given by it's k nearest neighbor as define by a distance metric. The parameter k is the only parameter that one needs to tweak in order to fit the data!


You can find the code for this here: https://github.com/yacineMahdid/artif...


Just check to README to figure out the exact location of the code. When I refactor the folder structure something it breaks the link so I prefer to just link the repository.

----
Join the Discord for general discussion:   / discord  

----
Follow Me Online Here:

Twitter:   / codethiscodeth1  
GitHub: https://github.com/yacineMahdid
LinkedIn:   / yacine-mahdid-809425163  
Instagram:   / yacine_mahdid  
___

Have a great week! 👋