Image Classification using K-Nearest Neighbors (KNN) on Fashion MNIST
Experiment
Title
Image Classification using K-Nearest Neighbors (KNN) on Fashion MNIST
๐ฏ Objective
- To implement KNN for multi-class image classification
- To experiment with different values of K
-
To analyze impact on:
- Accuracy
- Computational cost
๐ Dataset: Fashion MNIST
- 70,000 grayscale images (28×28 pixels)
- 10 classes:
| Label | Class |
|---|---|
| 0 | T-shirt/top |
| 1 | Trouser |
| 2 | Pullover |
| 3 | Dress |
| 4 | Coat |
| 5 | Sandal |
| 6 | Shirt |
| 7 | Sneaker |
| 8 | Bag |
| 9 | Ankle boot |
⚙️ Steps
- Load dataset
- Preprocess (flatten + normalize)
- Train KNN
- Evaluate performance
- Compare different K values
๐ป Complete Python Program
๐ Results
===== K = 1 =====
Accuracy: 0.8075
precision recall f1-score support
0 0.75 0.78 0.76 200
1 0.99 0.96 0.97 203
2 0.70 0.73 0.72 214
3 0.83 0.82 0.83 190
4 0.73 0.69 0.71 219
5 0.97 0.78 0.86 195
6 0.55 0.62 0.59 197
7 0.81 0.92 0.86 200
8 0.97 0.89 0.93 194
9 0.86 0.91 0.89 188
accuracy 0.81 2000
macro avg 0.82 0.81 0.81 2000
weighted avg 0.82 0.81 0.81 2000
===== K = 3 =====
Accuracy: 0.8195
precision recall f1-score support
0 0.68 0.83 0.75 200
1 0.99 0.95 0.97 203
2 0.71 0.79 0.75 214
3 0.89 0.83 0.86 190
4 0.78 0.69 0.73 219
5 0.97 0.79 0.88 195
6 0.57 0.54 0.56 197
7 0.86 0.93 0.89 200
8 0.98 0.90 0.94 194
9 0.86 0.96 0.91 188
accuracy 0.82 2000
macro avg 0.83 0.82 0.82 2000
weighted avg 0.83 0.82 0.82 2000
===== K = 5 =====
Accuracy: 0.8225
precision recall f1-score support
0 0.73 0.83 0.78 200
1 0.99 0.94 0.97 203
2 0.72 0.78 0.75 214
3 0.85 0.85 0.85 190
4 0.77 0.69 0.73 219
5 0.98 0.78 0.87 195
6 0.57 0.56 0.57 197
7 0.85 0.93 0.89 200
8 0.97 0.92 0.94 194
9 0.86 0.96 0.91 188
accuracy 0.82 2000
macro avg 0.83 0.82 0.82 2000
weighted avg 0.83 0.82 0.82 2000
๐ Observations
๐น K = 1
- Very sensitive to noise
- Overfitting
๐น K = 3
- Better generalization
- Balanced performance
๐น K = 5
- More stable
- Slight smoothing
๐ Visualization (Optional)
import matplotlib.pyplot as plt
# Display smaller and clearer images
for i in range(5):
plt.figure(figsize=(2,2)) # smaller figure size
plt.imshow(X_test[i].reshape(28,28), cmap='gray', interpolation='nearest')
plt.title(f"True: {y_test[i]}")
plt.axis('off') # remove axes
plt.show()
๐งช Lab Tasks
Task 1
Try larger K:
Task 2
Use full dataset → observe time
Task 3
Use weighted KNN:
Results
-
KNN works for image classification but:
- Computationally expensive
-
Optimal K improves:
- Accuracy
- Stability
Comments
Post a Comment