Lecture
Classification with K-Nearest Neighbors
K-Nearest Neighbors (KNN) is one of the simplest and most intuitive machine learning algorithms for classification tasks.
It predicts the class of a new data point by looking at the majority label among its nearest neighbors in the training data.
How KNN Works
- Store the entire training dataset.
- For a new data point:
- Calculate the distance to all training samples (commonly Euclidean distance).
- Select the
kclosest neighbors. - Assign the most common class among those neighbors.
Example: KNN on the Iris Dataset
Let’s use Scikit-learn to apply KNN classification to the classic Iris dataset.
KNN Classification Example
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import classification_report, accuracy_score # Load dataset iris = load_iris() X, y = iris.data, iris.target # Split data X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # Create KNN model knn = KNeighborsClassifier(n_neighbors=3) # Train the model knn.fit(X_train, y_train) # Predict y_pred = knn.predict(X_test) # Evaluate print(f"Accuracy: {accuracy_score(y_test, y_pred):.2f}") print("Classification Report:", classification_report(y_test, y_pred))
Choosing the Value of k
- Small
k→ captures local patterns but may be sensitive to noise. - Large
k→ produces smoother decision boundaries but may underfit.
A good approach is to try different k-values and choose the one that gives the best validation accuracy.
Key Takeaways
KNNis non-parametric — it doesn’t assume an underlying data distribution and requires no explicit model training.- Performs well on small to medium datasets, but can be computationally expensive for large ones.
- Always scale your features — KNN relies on distance, so unscaled data can distort results.
Lessons in this chapter · Machine Learning with Scikit-learn
- 1. Introduction to Scikit-learn
- 2. The Machine Learning Workflow
- 3. Types of ML - Supervised vs Unsupervised
- 4. Dataset Structure - Features and Labels
- 5. Splitting Data - Train vs Test
- 6. ML Workflow and Model Lifecycle
- 7. Feature Scaling and Preprocessing
- 8. Multiple-choice quiz
- 9. Classification with K-Nearest Neighbors
- 10. Regression with Linear Models
- 11. Evaluating Classification Models
- 12. Evaluating Regression Models
- 13. Introduction to Clustering (K-Means)
- 14. What is Cross-Validation?
- 15. Fill-in-the-blank quiz
Quiz
0 / 1
What does the K-Nearest Neighbors algorithm use to classify a new data point?
The sum of distances from all training samples.
The average value of the data points.
The majority class of its nearest neighbors.
The largest class in the entire dataset.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help