K-Nearest Neighbors (KNN) is a supervised machine learning algorithm that can be used for both classification and regression tasks. The algorithm works by finding the k closest data points in the training set for an unseen example or query point, and using the labels or values of those neighbors to make a prediction for the query point.
To illustrate how the algorithm works, letβs consider a simple binary classification problem where we want to classify whether a fruit is an apple or an orange based on its weight and color. We have a training set with weights and colors of various fruits, and their corresponding labels. Suppose we have a new fruit, and we want to predict whether it is an apple or an orange. We can use KNN to do this:
1. Choose a value for k (the number of neighbors to consider).
2. Calculate the distance (e.g., Euclidean distance) between the query point and all points in the training set.
3. Select the k data points in the training set with the smallest distances to the query point.
4. For classification tasks, take a majority vote of the labels of the k neighbors, and assign that label to the query point. For regression tasks, take the average value of the k neighbors and assign that value to the query point.
For example, letβs say we choose k=3, and we have the following training set:
| Weight | Color | Label |
|βββ|ββ-|ββ-|
| 100g | Red | Apple |
| 150g | Orange | Orange |
| 200g | Red | Apple |
| 180g | Orange | Orange |
| 120g | Red | Apple |
Now suppose we have a new fruit that weighs 160g and is red. We can use KNN to predict its label:
1. k=3
2. Calculate distances:
| Weight | Color | Distance |
|βββ|ββ-|βββ-|
| 100g | Red | 60 |
| 200g | Red | 40 |
| 120g | Red | 40 |
| 150g | Orange | >60 |
| 180g | Orange | >60 |
3. Select k=3 closest data points:
| Weight | Color | Distance |
|βββ|ββ-|βββ-|
| 200g | Red | 40 |
| 120g | Red | 40 |
| 100g | Red | 60 |
4. Majority vote of labels: Apple, Apple, Apple. Therefore, we predict that the new fruit is an apple.
KNN has some advantages and disadvantages. One advantage is that it is easy to understand and implement, and can work well for simple classification and regression tasks. Another advantage is that it can handle noisy data well. However, one downside is that it can be computationally expensive if the training set is large or the number of neighbors k is large. KNN can also be sensitive to the choice of distance metric and the scaling of the features.