Skip to content

submarinejuice/CNN-on-MNIST-Feature-Extraction-KNN-and-PCA-Analysis

Repository files navigation

🧠 CNN on MNIST: Feature Extraction, KNN, and PCA Analysis

A machine learning project exploring CNN-based feature learning, K-Nearest Neighbors classification, and PCA visualization on the MNIST dataset. By Michelle Chala

📌 Overview

This project trains a Convolutional Neural Network (CNN) to classify handwritten digits from the MNIST dataset, then extracts features from the trained CNN to:

Evaluate downstream classification using K-Nearest Neighbors (kNN) Visualize learned representations with Principal Component Analysis (PCA) The workflow combines supervised deep learning with unsupervised dimensionality reduction, offering insight into both classification performance and internal feature structure.

🛠️ Tools & Technologies

Python, NumPy, Pandas TensorFlow / Keras Matplotlib, Seaborn Scikit-learn (for kNN & PCA) 📈 CNN Architecture & Training

Input: 28x28 grayscale images Conv1: 8 filters, 3×3 kernel, ReLU, same padding Pooling Conv2: 16 filters, 3×3 kernel, ReLU Pooling Flatten → Dense (10 classes) Optimizer: Adam Loss: Sparse categorical crossentropy Epochs: 100 Train Accuracy: ~99.6% Test Accuracy: ~98.27%

🧪 Evaluation & Analysis

🔍 Confusion Matrix

Strong diagonal dominance Most errors occurred between similar digits (e.g., 3s vs 2s, 6s vs 5s) image

🔗 Feature Extraction

784-dimensional feature vectors extracted post-flattening kNN (k=5) on CNN features: ~98% accuracy Confirms CNN learned highly discriminative representations

🌀 PCA Visualization

Reduced to 2D for plotting: clear digit clusters with minimal overlap Reduced to 10D for classification: kNN accuracy: ~95.7% Slight performance drop due to information compression, but still strong image

🎯 Key Takeaways

CNNs not only perform well in classification but generate high-quality latent features Even simple models (like kNN) can succeed when built on well-learned representations PCA offers an intuitive visual confirmation of digit separability in feature space

🙋‍♀️ Author

Michelle Chala Undergraduate in Computer Science & Psychology, Concentration in Computational Modelling & Cognitive Neuroscience Focus: AI, Machine Learning, Cognitive Neuroscience, Quantum Computing

About

The project implements a CNN (convolutional neural network) to classify the MNIST dataset of handwritten digits. We used an input layer of 28x28 grayscale images, 8 filters with 3x3 kernels, ReLU activation, and ‘same’ padding for the first convolutional layer as outlined in the code.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors