Back to Resources
Machine Learning 10 Slides
K-Nearest Neighbors - Learning by Proximity.
Use ← and → to navigate
Swipe left / right on mobile
Guide Notes & Explanation
Accompanying breakdown for this slide deck
- K-Nearest Neighbors - Learning by Proximity
What is KNN?
- K-Nearest Neighbors is a simple, versatile machine learning algorithm. It can be used for both classification and regression tasks. Its core principle is that similar data points exist close to each other.
- It is called an instance-based or lazy learning algorithm. This is because it doesn't build a model during a training phase. Instead, it memorizes the entire training dataset and makes predictions only when needed.
- This approach makes training very fast, as it's just storing data. However, prediction can be slower for large datasets because it requires comparing the new point to every stored example.
How It Works
- Imagine a new, unlabeled data point arrives. The algorithm's first step is to find a predefined number of training examples closest to this new point. These are its "nearest neighbors."
- The distance to these neighbors is typically calculated using a distance metric. The most common choice is Euclidean distance, which is the straight-line distance between two points in space.
- The value of K, which you choose, determines how many neighbors are consulted. For example, if K=3, the algorithm finds the three closest data points to the new input.
The K Value
- The choice of K is the most critical setting in the KNN algorithm. It controls the balance between making a model that is too specific or too general.
- A small K value (like 1) makes the model very sensitive to noise. The prediction will be based on a single, closest point, which might be an outlier. This leads to a complex model with high variance.
- A large K value makes the model more stable by averaging over more points. However, it can oversimplify the decision boundary and ignore important local patterns. This results in a model with high bias.
Classification Task
- In a classification task, KNN is used to predict a discrete class label. After finding the K nearest neighbors, the algorithm looks at their classes.
- It then assigns the class that is most frequent among those neighbors. This is essentially a majority vote. The new data point is classified based on the consensus of its closest examples.
- For instance, if K=5 and three neighbors are "Cat" and two are "Dog", the new point will be classified as "Cat". This simple voting mechanism is powerful for many problems.
Regression Task
- KNN can also perform regression to predict a continuous value. The process of finding the nearest neighbors remains exactly the same.
- Instead of holding a vote, the algorithm calculates the average of the target values of the K neighbors. This average value becomes the prediction for the new data point.
- For a more nuanced approach, a weighted average can be used. Closer neighbors can be given more importance in the calculation than farther ones, making the prediction more refined.
Distance Metrics
- The concept of "nearest" is defined by a distance metric. The Euclidean distance is the most standard measure, calculated as the square root of the sum of squared differences.
- Euclidean Distance = sqrt( (x2-x1)^2 + (y2-y1)^2 )
- Another common metric is Manhattan distance, which sums the absolute differences along each dimension. It's like measuring the distance between two city blocks.
- Manhattan Distance = |x2-x1| + |y2-y1|
- The choice of metric depends on the nature of the data and the problem.
Pros and Cons
- KNN's main advantage is its simplicity and effectiveness. It is easy to understand and implement, and often works surprisingly well for complex problems. There are no assumptions about the underlying data distribution.
- However, it becomes very slow as the size of the training data grows. Prediction time increases because it must compute the distance to every single training example for each new prediction.
- It is also sensitive to irrelevant features and the scale of the data. Features on a larger scale can disproportionately influence the distance calculation, so data normalization is crucial.
Key Takeaways
- KNN is a fundamental algorithm that relies on the idea of similarity. It is powerful because of its simplicity and lack of required mathematical assumptions.
- Remember to carefully choose your K value and preprocess your data. Normalizing features and handling irrelevant data will significantly improve your model's performance.
- It serves as a great starting point for many machine learning problems. While it has limitations with large datasets, its intuitive nature makes it a vital tool for any data scientist's toolkit.