🟣 ML + AI · Lesson 55
K-Nearest Neighbors (KNN)
What is K-Nearest Neighbors?
K-Nearest Neighbors means kNN predicts the class of a new point by checking the nearest known data points.
In real programs, this topic helps in similarity-based classification. Learn the idea first, then type the program yourself and compare the output.
💡 At a Glance
| Point | Details |
|---|---|
| Course Area | Machine Learning + AI Concepts used for prediction, classification, clustering and AI-based projects. |
| Main Use | similarity-based classification |
| Example File | knn.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for similarity-based classification.
- It connects with recommendation basics.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| Term | Meaning |
|---|---|
| distance | Measure of closeness between data points. |
| neighbors | Nearest known points used by KNN for prediction. |
| k value | Number of neighbors checked by KNN. |
| classification | Predicting a category or class. |
| lazy learning | Learning style where prediction uses stored training data directly. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
Basic Pattern
from sklearn.neighbors import KNeighborsClassifier X = [[1], [2], [8], [9]] y = ["Low", "Low", "High", "High"] model = KNeighborsClassifier(n_neighbors=3) model.fit(X, y) print(model.predict([[7]])[0])
Complete Example Program
Python – knn.py
from sklearn.neighbors import KNeighborsClassifier X = [[1], [2], [8], [9]] y = ["Low", "Low", "High", "High"] model = KNeighborsClassifier(n_neighbors=3) model.fit(X, y) print(model.predict([[7]])[0])
Expected Output
High
Program Explanation
from sklearn.neighbors import KNeighborsClassifierimports ready-made features from a module/library.X = [[1], [2], [8], [9]]stores a value in X.y = ["Low", "Low", "High", "High"]stores a value in y.model = KNeighborsClassifier(n_neighbors=3)stores a value in model.model.fit(X, y)performs the next step of the program logic.print(model.predict([[7]])[0])displays information or calculated result on the screen.
Where will you use it?
- Similarity-based classification.
- Recommendation basics.
- Pattern recognition.
Common Mistakes
- Training and testing the model on the same data.
- Using an algorithm without understanding the input features.
- Reporting only accuracy without checking actual mistakes and limitations.
Practice Tasks
- Type the program in
knn.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to similarity-based classification.
- Write 5 lines explaining the logic in your own words.
Summary
K-Nearest Neighbors is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.