Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 62

Implementing k-NN for Classification and Regression

Recommended resources

Hello! Let's dive into the next topic in our "Foundations of Machine Learning" module.

In our last lesson, we explored linear regression and optimized its parameters using gradient descent. This is a classic example of a parametric model, where the goal is to learn a fixed number of parameters (like the slope and intercept ) that define a specific function—in that case, a line.

Today, we're going to explore a completely different approach. Your learning outcome is to implement the k-Nearest Neighbors (k-NN) algorithm for classification and regression. K-NN is a non-parametric, or instance-based, learning algorithm. Instead of learning a function, it memorizes the entire training dataset and makes predictions based on the similarity between a new, unseen data point and the existing data points. It's one of the simplest and most intuitive machine learning algorithms you'll encounter.

1. The Core Idea: Prediction by Proximity

The fundamental principle behind k-NN is that "things that are similar are close to each other." To predict the label or value of a new data point, the algorithm looks at the 'k' most similar data points (its "nearest neighbors") from the training set and uses them to make a decision.

Let's start by understanding this for a classification task, where we're predicting a category. The following video provides an excellent visual introduction.

Machine Learning Tutorial Python - 18: K nearest neighbors classification with python code

This video, 'K nearest neighbors classification with python code' by codebasics, clearly explains the intuition behind k-NN using a simple flower classification example. Pay close attention to how the value of 'k' influences the outcome.

Please watch the first part of the video, from the beginning up to 03:29. Focus on: How a new data point is classified based on its neighbors. The concept of 'k' and why choosing the right value is important. The idea of using distance to determine which points are 'nearest'.

As you saw, the process involves three key elements:

  1. The 'k': This is a number you choose. It tells the algorithm how many neighbors to look at. For example, if k=5, the algorithm will find the 5 closest points in the training data.

  2. Distance Metric: To find the "nearest" neighbors, we need to measure the distance between points. The most common method is the Euclidean distance, which is just the straight-line distance between two points. For two points, and , in an n-dimensional space, the formula is:

    This should feel familiar from your linear algebra studies, as it's the norm of the difference between two vectors: .

  3. Prediction Method (Classification): For classification, k-NN uses majority voting. It looks at the classes of the k nearest neighbors and assigns the new data point the class that appears most frequently among them.


2. Adapting k-NN for Regression

What if we want to predict a continuous numerical value instead of a class? This is a regression task. The k-NN algorithm adapts to this very simply.

  • Steps 1 and 2 (choosing 'k' and calculating distances) remain exactly the same.
  • Step 3, the prediction method, changes. Instead of a majority vote, k-NN for regression typically calculates the average of the values of its k nearest neighbors.

For a more detailed explanation of these core mechanics, you can refer to the following article.

The k-Nearest Neighbors (kNN) Algorithm in Python

The article 'The k-Nearest Neighbors (kNN) Algorithm in Python' from Real Python provides a fantastic from-scratch explanation that solidifies these concepts.

Please read the section titled 'A Step-by-Step kNN From Scratch in Python'. Focus on the subsections 'Define “Nearest” Using a Mathematical Definition of Distance' and 'Voting or Averaging of Multiple Neighbors'. This will reinforce the difference between the classification and regression prediction methods.

Test your understanding!

You have a new data point to predict. You've set k=5.

  1. Classification: The classes of the 5 nearest neighbors are: ['Blue', 'Red', 'Blue', 'Blue', 'Red']. What is the predicted class?
  2. Regression: The values of the 5 nearest neighbors are: [15, 20, 12, 18, 25]. What is the predicted value?
Show answer
  1. Classification: The prediction is 'Blue'. There are 3 'Blue' neighbors and 2 'Red' neighbors. 'Blue' is the majority class.
  2. Regression: The prediction is the average of the values: .

3. Implementing k-NN with scikit-learn

Now for the practical part. While it's great to understand the algorithm from scratch, in practice you'll use a library like scikit-learn. The workflow is very similar to what you've seen before.

Let's continue with the codebasics video, which now demonstrates how to implement a k-NN classifier in Python.

Machine Learning Tutorial Python - 18: K nearest neighbors classification with python code

This segment of the video walks through a full implementation using scikit-learn, from loading data to training and evaluating the model.

Please watch from 03:29 to 09:13. Pay attention to these steps: Loading and preparing the data using Pandas. Splitting the data into training and testing sets using train_test_split. Importing and creating the model: from sklearn.neighbors import KNeighborsClassifier. Training the model: knn.fit(X_train, y_train). Evaluating the model: knn.score(X_test, y_test).

The general steps for using a scikit-learn model are:

  1. Import the model class (e.g., KNeighborsClassifier or KNeighborsRegressor).
  2. Instantiate the model, setting its hyperparameters (like n_neighbors=k).
  3. Fit the model to the training data (.fit()).
  4. Predict on new data (.predict()).
  5. Evaluate the model's performance (.score()).

4. A Critical Step: Feature Scaling

There is a very important detail about k-NN. Because it's a distance-based algorithm, the scale of your features matters immensely.

Imagine you have two features: age (from 20 to 70) and income (from 30,000 to 150,000). A difference of 10 in age is tiny compared to a difference of 10,000 in income. When calculating Euclidean distance, the income feature will completely dominate the calculation, and the age feature will be almost irrelevant.

To prevent this, you must scale your features before applying k-NN. The most common methods are:

  • Standardization (StandardScaler): Rescales features to have a mean of 0 and a standard deviation of 1.
  • Normalization (MinMaxScaler): Rescales features to a specific range, usually [0, 1].

You've already been introduced to this in a previous lesson, and now you see a situation where it is not just good practice, but absolutely essential. The following video provides a dramatic demonstration of how scaling can rescue a failing k-NN model.

Introduction to kNN: k Nearest Neighbors Classification and Regression in Python Using scikit-learn

In this video, 'Introduction to kNN' by Ciara Feely, a k-NN regressor is first applied to the Boston Housing dataset without scaling and gets poor results. Then, a standard scaler is introduced, and the performance improves significantly.

Please watch from 16:22 to 21:30. You don't need to follow every line of code, but focus on the narrative: Note the initial high Mean Squared Error (MSE) and low R-squared score. See how StandardScaler is incorporated into a pipeline. Observe the dramatic improvement in the MSE and R-squared score after scaling is applied.


5. Choosing the Right 'k'

The value of k is the most important hyperparameter for a k-NN model. The choice of k involves a trade-off:

  • Small k (e.g., k=1): The model is very flexible and sensitive to noise. A single noisy data point can change the prediction boundary. This leads to low bias but high variance (overfitting).
  • Large k: The model is smoother and more resistant to noise, as it averages over many neighbors. However, it might miss local patterns in the data. This leads to high bias but low variance (underfitting).

So, how do we find the best k? The standard approach is to try a range of values for k, train a model for each, and evaluate their performance on a validation set. The k that gives the best validation performance is chosen. scikit-learn provides a tool called GridSearchCV that automates this process.

The k-Nearest Neighbors (kNN) Algorithm in Python

This final reading from the Real Python article shows you a systematic way to tune hyperparameters like 'k'. This is a key skill for practical machine learning and for your GATE preparation.

Please read the section 'Tune and Optimize kNN in Python Using scikit-learn'. Focus on the subsection 'Improving kNN Performances in scikit-learn Using GridSearchCV'. Understand how GridSearchCV is used to test a range of values for n_neighbors to find the optimal one.


Conclusion

You have now learned how to use k-Nearest Neighbors, a powerful yet simple algorithm. It provides a great contrast to the parametric linear regression model from our last lesson.

Key Takeaways:

  • k-NN is a non-parametric, instance-based algorithm used for both classification (majority vote) and regression (averaging).
  • It makes predictions based on the k closest data points in the training set.
  • Feature scaling is mandatory for k-NN because it is a distance-based algorithm.
  • The choice of k is a crucial hyperparameter that controls the bias-variance trade-off. It should be tuned using a validation set or cross-validation.
  • Despite its simplicity, k-NN can be a very effective model, especially with smaller, well-behaved datasets.

Preview of the Next Lesson:
In our next lesson, we will move on to another fundamental classification algorithm: Logistic Regression. While its name sounds like it's for regression, it's actually for classification. You will see how it uses a linear equation, similar to linear regression, but passes it through a special function (the sigmoid function) to produce probabilities, giving us a new way to model categorical outcomes.