Implementation of K-Nearest Neighbors (KNN) Classification Algorithm - Sample data Set
Experiment
Title
Implementation of K-Nearest Neighbors (KNN) Classification Algorithm
๐ฏ Objective
- To understand the working of KNN classification
- To classify data points based on nearest neighbors
- To study the effect of different values of K
๐ Background Theory
๐น KNN Algorithm
- A non-parametric, instance-based learning algorithm
- Classifies a new point based on majority vote of nearest neighbors
๐น Distance Measure (Euclidean Distance)
๐งฉ Sample Dataset
We classify whether a fruit is:
- ๐ Apple (0)
- ๐ Orange (1)
| Weight | Size | Class |
|---|---|---|
| 150 | 7 | Apple |
| 170 | 7.5 | Apple |
| 140 | 6.8 | Apple |
| 130 | 6.5 | Orange |
| 120 | 6.2 | Orange |
| 110 | 6 | Orange |
⚙️ Algorithm Steps
- Choose value of K
- Compute distance from test point to all training points
- Select K nearest neighbors
- Perform majority voting
- Assign class
๐ป Python Program
๐ Output
๐ Observations
- Classification depends on nearest neighbors
- Changing K changes prediction
- Smaller K → sensitive to noise
- Larger K → smoother decision
๐งช Lab Tasks
Task 1
Change value of K:
Task 2
Change test point:
๐ Key Insights
| K Value | Behavior |
|---|---|
| Small K | Overfitting |
| Large K | Underfitting |
Result
-
KNN is:
- Simple
- Easy to implement
- No training phase (lazy learner)
-
Works well for:
- Small datasets
Comments
Post a Comment