This project implements two machine learning classifiers, K-Nearest Neighbors (KNN) and Naive Bayes, to classify handwritten digits from the MNIST dataset. The project is structured to handle data loading, preprocessing, and classification, with a focus on simplicity and clarity.
MNIST-classifier/
├── src/
│ ├── classifiers/
│ │ ├── Classifier.py # Base classifier interface
│ │ ├── KNNClassifier.py # K-Nearest Neighbors implementation
│ │ └── NaiveBayesClassifier.py # Naive Bayes implementation
│ └── data/
│ ├── Dataset.py # Dataset handling utilities
│ └── MNISTDataScaler.py # Data preprocessing and scaling
├── data/
│ ├── train/
│ │ └── mnist_train.csv # Training data
│ └── test/
│ └── mnist_test.csv # Testing data
└── main.py # Main script to run classifiers
- Python 3.6+
- Required libraries:
numpy,pandas,scikit-learn
Install dependencies using:
pip install numpy pandas scikit-learn-
Prepare the Data:
- Place the MNIST dataset files (
mnist_train.csvandmnist_test.csv) in thedata/train/anddata/test/directories, respectively. - The dataset should have 784 feature columns for pixel values (unlabeled data) or 785 columns including the label (labeled data).
- Place the MNIST dataset files (
-
Run the Classifiers:
- The
main.pyscript runs both the Naive Bayes and KNN classifiers on the provided dataset. - Execute the script from the project root directory:
python main.py
- By default, the KNN classifier uses
k=5. You can modify thekvalue in themain.pyscript if needed.
- The
-
Output:
- The script will output:
- The classifier name.
- Indices where predictions differ from expected labels, showing expected and predicted values.
- The number of correct predictions and total samples.
- The accuracy percentage.
- The computation time in minutes.
- The script will output:
-
Naive Bayes (
NaiveBayesClassifier.py):- Implements a Naive Bayes classifier with binarization (threshold at 128) and Laplace smoothing to handle zero probabilities.
- Assumes 10 classes (digits 0-9) and 784 features (28x28 pixel images).
-
K-Nearest Neighbors (
KNNClassifier.py):- Implements a KNN classifier with Euclidean distance and weighted voting based on inverse distances.
- Uses
MNISTDataScalerto normalize pixel values to the range [0, 1]. - Adjusts
kto an odd number if an even value is provided to avoid ties.
-
Dataset.py:
- Provides a
Datasetclass to load and manage labeled or unlabeled MNIST data from CSV files. - Automatically detects whether the data is labeled (785 columns) or unlabeled (784 columns).
- Provides a
-
MNISTDataScaler.py:
- Scales pixel values from [0, 255] to [0, 1] for KNN classification.
- Supports forward and inverse transformations.
The following table compares the performance of the Naive Bayes and KNN classifiers on the MNIST test dataset (10,000 samples). Performance metrics are based on typical runs with the provided implementation and may vary slightly depending on system specifications.
| Classifier | Accuracy (%) | Runtime (minutes) | Notes |
|---|---|---|---|
| Naive Bayes | ~83-85 | ~0.5-1.0 | Fast, uses binarized features |
| KNN (k=5) | ~96-97 | ~5-10 | Slower, uses normalized features |
Notes:
- Accuracy values are approximate and depend on the specific MNIST dataset split.
- Runtime is measured on a standard CPU; KNN is computationally intensive due to distance calculations.
- Naive Bayes is faster but less accurate due to its simplifying assumptions.
- KNN achieves higher accuracy but requires more computation time, especially for larger
kvalues.
- The project assumes the MNIST dataset is in CSV format with pixel values as unsigned 8-bit integers.
- The
Classifier.pyfile defines an abstract base class for classifiers, ensuring a consistent interface forfit,predict, andevaluatemethods. - Logging is enabled in
KNNClassifier.pyto track prediction progress.