The MNIST database of handwritten digits has a training set of 60,000 examples, and a test set of 10,000 examples. It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image.
The original black and white (bilevel) images from NIST were size normalized to fit in a 20x20 pixel box while preserving their aspect ratio. The resulting images contain grey levels as a result of the anti-aliasing technique used by the normalization algorithm. the images were centered in a 28x28 image by computing the center of mass of the pixels, and translating the image so as to position this point at the center of the 28x28 field.
With some classification methods (particularly template-based methods, such as SVM and K-nearest neighbors), the error rate improves when the digits are centered by bounding box rather than center of mass.
The challenge in this task divided into 4 parts, followed by specific instructions. My full Solution written in Python Jupyter-Notebook can be downloaded directly.
The beginning of the task:
Preprocess the data. Visualize one element from each class. Visualize the mean of each class.
My solution included Looping over X_images and y_train ten times (for each class/digit) and Looking for the index (named ‘location’) in y_train, then extracting one of the proper figures from X_images.
My Solution for the means visualization started with combining X_image, y_train into a zipped list. The zipped list contain pairs of: one item from X_images, and one item from y_train.
The second part of my solution include Building a new dictionary with a loop over the zipped list. The new dictionary will be used later for the means calculations.
The last part include Looping and plotting the mean of every class (digit).
Try fitting a logistic regression with its solver set to be the ‘Ibfgs’ algorithm. (If you’d like, you can try the other solvers/optimizers and observe the differences in computation time.)
What does reducing the dimensionality do to the computation time and why? What does reducing the number of data points do to the computation time and why? List one advantage and disadvantage of reducing dimensionality. List one advantage and disadvantage of reducing the number of data points.
My Solution: This is a multiple class case using a Logistic Regression model; hence, the binary model creates a separate model for each class and choose the best one for the prediction, according to probabilities. (This is the disadvantage of using Logistic Regression model on multiple classes)
Reducing the dimensionality by slicing and reducing the number of columns, the computation time is (naturally) lower since we reduced the data size. The reason is that we working now on a smaller dataset.
Reducing the number of data points reduce the computation time as well, for the same reason, the solver works on a lower number of data points. Hence, the computation time is shorter and the accuracy is lower too.
one advantage for reducing dimensionality: a faster computation time. one disadvantage for reducing dimensionality: we can miss some essential columns from the data resulting in a non-accurate model. However, if we reduce the number dimensionality by reducing the components as an output of the PCA method, that way, we reduce the dimensionality and focus on the most critical components.
one advantage of reducing the number of data points: A faster computation time. one disadvantage of reducing the number of data points: if we do that without planning and thinking in advanced, we can (randomly) reject essential points from the dataset.
Use 5-fold cross-validation with a KNN classifier to model the data. Try various values of k (e.g. from 1 to 15) to find the ideal number of neighbors for the model. What kind of accuracy are you getting? If you find this is taking too long for your computer you can subset the data to reduce the number of training points. What happens to train and validation set accuracy if you set the K in your K-NN model to 1 (1-Nearest Neighbours) or to the number of training points (60000-Nearest Neighbours). Can you explain what is going on and why it is happening? Answer the previous question again but using decision trees where instead of controlling for K we control for the depth of the tree.
My Solution: 5-fold cross validation divides the data into five parts, train on 4 slices and test it on the last one. That will happen five times (5 combinations of the split)
The loop ideally runs for each value between 1~15 and plots at the end the ideal number of neighbours for the model.
This is not a full run since a full run takes a long run-time. (did not complete after 24 hours)
Hence, the choice is a randomly chosen 1000 out of the all, a portion of the dataset that runs in a reasonable run-time. the accuracy here at the pick point (around 5 neighbors) is close to 87%.
What happens to the accuracy if you set the K in KNN to 1?
Setting K=1 will give 100% accuracy if the model will be trained on the all dataset since we will always find one that is the closest to itself, in a case of a train set as a portion, the accuracy will not be 100% on the test set.
What will happens to the accuracy if you set the k in KNN to 60000?
How many we have from each class? Creating a pd on y (below) reveals that there are more from class ‘1’ compared to any other class. Hence, Setting 60000 near neighbours, the highest value is ‘1’ so the algo will always choose ‘1’ in this case to classify the point, the class with the highest number of images.
Changing to decision trees and looping over the depth of the tree reveals a graph that shows a good result in depth 10, from 10 forwards the accuracy became slightly lower.
Fit a linear model, such as logistic regression or an SVM (to speed up training you can modify the default settings). Try to get as high an accuracy as possible on a validation set. What does the class confusion matrix look like for your best model? Is there anything that stands out in it? Re-fit a linear model that can discriminate between the digit 4 and the digit 9. Visualize the weights of the model as an image, does anything stand out?
My Solution: Starting with a Logistic Regression Model.
Here I created a second y_predict = log.predict(X_test) and passed it into the matrix, with the real value. (y_test)
The diagonal line in the Confusion Matrix stands out, the values are higher compared to the others. The higher values related to our (relatively) high score.
Other than the diagonal, the Inaccuracies values are relatively low, at column 8 row 2 there is an Inaccuracy of 119 for example.
Regarding the question: Re-fit a linear model that can discriminate between the digit 4 and the digit 9 and visualise, I Created a variable that will contain only True and False for the relevant digits: 4 and 9:
Interpretation: The yellow dots on the bottom (mainly) imply a magnitude values, these are the points that the algorithm can distinguish between the values 4 and 9.
There is no presentation to both digits at the bottom, where some of the yellow points located.
Your goal is to train a model that maximizes the predictive performance (accuracy in this case) on this task. Optimize your model’s hyperparameters if it has any. Give evidence why you believe the hyperparameters that you found are the best ones. Provide visualizations that demonstrate the model’s performance.
My Solution: Starting with a Logistic Model, the hyperparameters there are the regularisation penalty values (and others), but the run time is slow, and I’m not sure that Logistic is the right fit for this model.
Another try made with KNN model, the run time delays the flow of the work.
So, the choice here was to train a decision tree, starting with the hyperparameters best values (for few selected hyperparameters), continue with splitting the train portion into a train and validate portions. In the case of a decision tree, the validation portion will help to asses the optimal Depth of the tree.
I started with SOME Hyperparameters Tuning for a decision tree, Setup the parameters and the distributions to sample, from: param_dist
There are few hyperparameters for a decision tree, we found the best value for few of them (some combination) with an exception for the max_depth (returns ‘None’), hence another check is needed.
I created a prediction for the test set results and continued with Visualizing the results with a confusion matrix with the code:
The outcome Confusion Matrix:
The confusion matrix values are normal for this kind of accuracy, the diagonal values are the highest, with relatively low values else that relates to model Inaccuracies.
The max_depth value output from the previous step is not satisfying, hence another run and plot are needed to evaluate the optimal value of the depth. Start with another split, a split of the train into: train, validate.
Plot the accuracy scores of the train portion along the validation portion scores:
From the plot we can see that Depth 5 Looks like an appropriate point, and higher values can lead to overfitting.
Accourding to the results, I decided to build a Model with the combination of hyperparameters as follow:
This article, along with any associated source code and files, is licensed under GPL. (GPLv3)