Introduction
Welcome back! In our previous lesson, we dissected the theory behind the k-Nearest Neighbors (KNN) algorithm. You learned that it's a "lazy learner" that classifies new data points based on a simple, intuitive principle: similarity. It identifies the 'k' most similar data points from the training set—its "neighbors"—and uses a majority vote among them to make a prediction. We also discussed the crucial role of the hyperparameter 'k' and the distance metrics used to quantify "nearness."
Today, we transition from theory to practice. Your learning outcome is to implement the KNN algorithm for classification using scikit-learn. We will apply the standard machine learning workflow you've practiced with other models, but with a special focus on the unique requirements of a distance-based algorithm like KNN. This lesson will equip you with the practical skills to build, evaluate, and tune a KNN model, a fundamental tool in any AI practitioner's toolkit.
The Importance of Feature Scaling
Before we write any code, we must address a critical data preparation step for KNN: feature scaling.
In the last lesson, we established that KNN relies on calculating distances (e.g., Euclidean distance) to find the nearest neighbors. Now, consider a dataset with two features:
age(ranging from 20 to 70)income(ranging from 30,000 to 150,000)
The scale of income is vastly larger than age. When we calculate the Euclidean distance, the income feature will completely dominate the calculation, and the age feature will have almost no influence on which neighbors are considered "close."
A small difference in income will overwhelm a large difference in age. This is problematic because it implicitly gives one feature much more importance than another, which is not what we want.
Feature scaling solves this by transforming the data so that all features are on a comparable scale. A common method is standardization, where each feature is rescaled to have a mean of 0 and a standard deviation of 1.
K-Nearest Neighbors (KNN) Classification with scikit-learn
The following article, 'K-Nearest Neighbors (KNN) Classification with scikit-learn,' clearly explains why normalization is essential for KNN and when to apply it in the workflow.
Please read the section titled Normalizing & Splitting the Data. Focus on the paragraph explaining why normalization is crucial for distance-based algorithms and the importance of scaling after splitting the data to avoid data leakage.
As the article highlights, scaling is performed after the train-test split. You fit the scaler on the training data to learn the scaling parameters (mean and standard deviation) and then use those same parameters to transform both the training and test data. This prevents any information from the test set from "leaking" into your training process.
Implementing KNN with Scikit-Learn: A Walkthrough
Now, let's build our KNN classifier. We'll use the classic Iris flower dataset, where the goal is to classify a flower into one of three species based on its sepal and petal measurements.
The following video provides an excellent end-to-end walkthrough of the process in Python. We will go through it step-by-step.
Step 1: Data Preparation and Splitting
First, we load the dataset, create a pandas DataFrame for easier manipulation, and split our data into features (X) and the target variable (y). Then, we use train_test_split to create our training and testing sets. This workflow should be very familiar to you by now.
Machine Learning Tutorial Python - 18: K nearest neighbors classification with python code
This segment from the 'codebasics' channel covers the initial data setup.
Watch from 03:29 to 06:51. Observe how the Iris dataset is loaded from scikit-learn, structured in a pandas DataFrame, and then split into training and testing sets.
Note: For simplicity, the video does not apply feature scaling. However, in most real-world scenarios, and as we just discussed, you would add a scaling step right after the train_test_split.
Step 2: Training the KNN Classifier
This is the core of our lesson. With our data prepared, we can now import the KNeighborsClassifier from scikit-learn, create an instance of it, and train it on our data.
Training a KNN model is surprisingly simple. The .fit() method for KNN primarily consists of loading the training data into the model's memory. All the heavy computational work of calculating distances happens during prediction.
Machine Learning Tutorial Python - 18: K nearest neighbors classification with python code
Let's watch how to instantiate and train the KNN model.
Watch from 06:51 to 09:13. Pay close attention to: How KNeighborsClassifier is imported. The instantiation of the model: KNeighborsClassifier(n_neighbors=3). Note that n_neighbors is the scikit-learn parameter for our hyperparameter 'k'. The call to knn.fit(X_train, y_train) to 'train' the model.
The key line of code is:
from sklearn.neighbors import KNeighborsClassifier
# Instantiate the model with k=3
knn = KNeighborsClassifier(n_neighbors=3)
# Fit the model on the training data
knn.fit(X_train, y_train)
Step 3: Evaluation
Once the model is fitted, we can evaluate its performance on the unseen test data. We'll use the .score() method for a quick accuracy check, and then generate a confusion_matrix and classification_report for a more detailed performance breakdown.
Machine Learning Tutorial Python - 18: K nearest neighbors classification with python code
Now, let's see how well our trained model performs.
Watch from 09:13 to 13:34. Follow the process of: Calculating the model's accuracy on the test set. Generating and interpreting the confusion matrix. Remember, the diagonal represents correct predictions. Generating and reading the classification report, which gives you precision, recall, and F1-score for each class.
These evaluation tools are the same ones you used for logistic regression. This consistency is one of the great strengths of the scikit-learn library—it provides a unified interface for many different algorithms.
Finding the Optimal 'k'
In the video, the value of k (or n_neighbors) was chosen arbitrarily. But as we discussed in the last lesson, the choice of k is critical. How do we find the best value? We can do this experimentally.
The standard approach is to:
- Define a range of
kvalues to test (e.g., 1 to 20). - Loop through each
kvalue. - For each
k, train a new KNN model. - Evaluate its performance, typically using k-fold cross-validation on the training set to get a robust estimate of performance.
- Plot the performance metric (e.g., accuracy) against the
kvalues. - Choose the
kthat yields the best performance.
This process is a form of hyperparameter tuning.
k-nearest neighbor algorithm using Sklearn
This article from GeeksforGeeks provides a clear explanation and code example for finding the best 'k' using cross-validation.
Read the section 4. Cross-Validation to Choose Best k. Study the Python code provided. It shows exactly how to loop through a range of k values, use cross_val_score to evaluate each one, and plot the results to find the optimal k.
By systematically testing different values for k, you move from guessing to making an evidence-based decision, a hallmark of a skilled machine learning practitioner.
Conclusion
In this lesson, you have successfully translated the theory of K-Nearest Neighbors into a working Python implementation. You've seen how to use scikit-learn to prepare data, train a KNeighborsClassifier, and evaluate its performance. Most importantly, you learned about two aspects critical to KNN: the necessity of feature scaling and a systematic method for tuning the hyperparameter 'k'.
Key Takeaways:
- KNN is implemented in
scikit-learnusing theKNeighborsClassifier. - Feature scaling is a mandatory preprocessing step for distance-based algorithms like KNN to ensure all features contribute fairly to the distance calculation.
- The hyperparameter 'k' is set using the
n_neighborsargument. - The optimal 'k' can be found by iterating through a range of values and using cross-validation to identify which
kproduces the most robust model. - The rest of the workflow—splitting data, fitting the model, and using metrics like accuracy, confusion matrix, and classification report—remains consistent with other
scikit-learnmodels.
Preview of the Next Lesson:
We will now move on to another powerful and highly interpretable classification model: Decision Trees. Unlike the distance-based logic of KNN, decision trees make predictions by learning a set of hierarchical if-then-else rules from the data, creating a model that is easy to visualize and understand.