The K-Nearest Neighbor (KNN) algorithm is a simple, instance-based machine learning classifier that operates on the principle that similar data points are located near each other in feature space; it classifies new data points by identifying the K nearest neighbors from the training dataset and assigning the majority class among them, requiring two key parameters: a distance metric (such as Euclidean or Manhattan distance) to measure proximity and a K value that determines how many neighbors to consider, with lower K values risking overfitting and higher K values potentially smoothing predictions but reducing accuracy on noisy data.
K-Nearest Neighbor Algorithm: Classification & Regression Guide
Added:whether you're just getting started on your journey to becoming a data scientist or you've been here for years you'll probably recognize the K NN algorithm it stands for K nearest neighbors and it's one of the most popular and simplest classification and regression classifiers used in machine learning today as a classification algorithm KNN operates on the assumption that similar data points are located near each other and can be grouped in the same category based on their proximity so let's consider an example imagine we have a data set containing information about different types of fruit so let's visualize our fruit data set here now we have each fruit categorized by two things we have it categorized by its sweetness that's our x axis here and then on the Y AIS we are classifying it by its crunchiness now we've already labeled some data points so we've got a a few apples here apples are very crunchy and somewhat sweet and then we have a few oranges down here oranges are very sweet not so crunchy now suppose you have a new fruit that you want to classify well we measure it's crunchiness and we measure its sweetness and then we can plot it on the graph let's say it comes out maybe here the K&N algorithm will then look at the K nearest points on the graph to this new fruit and if most of these nearest points are classified as apples the algorithm will classify the new fruit as an apple as well how's that for an Apples to Apples comparison now before a classification can be made the distance must be defined and there are only two requirements for a KNN algorithm to achieve its goal and the first one is What's called the distance metric the distance between the query point and the other data points needs to be calculated fing decision boundaries and partitioning query points into different regions which are commonly visualized using Veron diagrams which kind of look like a kaleidoscope and this distance serves as our distance metric and can be calculated using various measures such as ukian distance or Manhattan distance so that's number one number two is we need now need to define the value of K and the K value in the KNN algorithm defines how many neighbors will be checked to determine the classification of a specific query point so for example if k equals 1 the instance will be assigned to the same class as its single nearest neighbor choosing the right K value largely depends on the input data data with more outliers or noise will likely perform much better with higher values of K also it's recommended to choose an odd number for K to minimize the chances of ties in classification now just like any machine learning algorithm KNN has its strengths and it has its weaknesses so let's take a look at some of those and on the plus side we have to say that knnn is quite easy to implement its Simplicity and its accuracy make it one of the first classifiers that a new data scientist will learn it also has only a few hyper parameters which is a big advantage as well KNN only requires a k value and a distance metric which is a lot less than other machine learning algorithms also in the plus category we can say that it's very adaptable as well meaning as new training samples are added the algorithm adjusts to account for any new data since all training data is stored into memory that sounds good but there's also a drawback here and that is but because of that it doesn't scale very well as a data set grows the algorithm becomes less efficient due to increased computational complexity comprising compromising the overall model performance and this this inability to scale it comes from KNN being what's called a lazy algorithm meaning it stores all training data and defers the computation to the time of classification that results in higher memory usage and slower processing compared to other classifiers now KNN also tends to fall victim to something called The Curse of dimensionality which means it doesn't perform well with high dimensional data inputs so in our sweetness to crunchiness example this is a 2d space it's relatively easy to find the nearest neighbors and classify new fruits accurately however if we keep adding more features like color and size and weight and so on the data points become sparse in the high dimensional space the distances between the points starts to become similar making it difficult for K&N to find meaningful neighbors and it can also lead to something called the peaking phenomenon where after reaching an optimal number of features adding more features just increases noise and increases classification errors especially when the sample size is small feature selection and dimensionality reduction techniques can help minimize the curse of dimensionality but if not done carefully they can make KNN prone to another downside and that is the downside of over fitting lower values of K can overfit the data whereas higher values of K tend to smooth out the prediction values since it's averaging the values over a greater area or neighborhood so because of all this the KNN algorithm is commonly used for simple recommendation systems so for example the algorithm can be applied in the areas of data preprocessing that's pretty common use case for knnn and that's because the KNN algorithm is helpful for data sets with missing values since it can estimate for those values using a process known as missing data imputation now another use case is in Final in in the KNN algorithm it's often used in stock market forecasting currency exchange rates trading Futures and moneya laundering analysis money laundering analysis and also we have to consider the use case for healthcare it's been used to make predictions on the risk of heart attacks and prostate cancer by calculating the most likely Gene Expressions so that's KNN a simple but imperfect classification and regression classifier in the right context it's straightforward approach is as delightful as biting into a perfectly classified Apple if you like this video and want to see more like it please like And subscribe if you have any questions or want to share your thoughts about this topic please leave a comment below
Up Next

Evaluation and Cross-Validation in Machine Learning
@machinelearning-sudeshnasa3607
156.5K views•2016-06-28

Building Real-Time ML Pipelines with Feature Stores and MLOps Frameworks
@ODSCAI
5.1K views•2022-02-20

Bypassing Tor Censorship: Bridges and Pluggable Transport Guide
@Coding_ForEveryone
397 views•2024-06-11

Neural Networks Explained: Math, Layers, and Learning Fundamentals
@3blue1brown
21.9M views•2017-10-05
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Artificial Intelligence


![#4 Machine Learning Specialization [Course 1, Week 1, Lesson 2]](https://i.ytimg.com/vi/sca5rQ9x1cA/maxresdefault.jpg)





![[핵심 머신러닝] K-nearest neighbors & Distance Measures](https://i.ytimg.com/vi_webp/W-DNu8nardo/maxresdefault.webp)







![[ Part 2 ] Pelatihan Data Science Beginner Level | Digital Talent Hub | PPI Dunia | Modul 4](https://i.ytimg.com/vi/jL5AhoepKeU/maxresdefault.jpg)






















