AI/ML

Handling Digit Recognizer dataset without deep learning

Digit recognizer dataset to deep learning enthusiast is what printing “Hello World” means to a coder. All theories of deep learning could initially practiced on this dataset. This dataset contains a sample of hand written digits ranging from [0-9] and their quantity is almost equally distributed in the dataset. I have prepared a notebook for getting people familiarized with this dataset without using any concepts of deep learning. Lets get started..

The first step is to familiarize the dataset , one could see a total of 785 columns in the train dataset. The first column is the target variable informing us about the digit written in the image and next 784 columns are the pixels of 28×28(28×28=784) . We try to see how many images each digit have and to our surprise its almost equal .

Next we would like to visualize the images from the pixels . Lets see how the first 50 images looks like , we do this with the help of a little python code

df_train = df_train.values.reshape(-1,28,28,1)  #1
test = test.values.reshape(-1,28,28,1)          #2


#Lets display first 50 images
plt.figure(figsize=(15,8))
for i in range(50):
    plt.subplot(5,10,i+1)
    plt.imshow(df_train[i].reshape((28,28)),cmap='binary')
    plt.axis("off")
plt.tight_layout()
plt.show()

Let me try to explain what above reshape means in line #1 and #2
We have got ourselves a 28×28 pixel image whose pixel values are all stacked in a single row.
In order to view these 784 column values as an image we convert it into a 28x28x1 matrix , here 1 stands for number of color channels, if we had a colored picture we would have used 3
Finally the value (-1) , -1 in a reshape function means you do not have to worry about the dimension , the function will calculate it for you. In our case -1 represents the no of rows of our dataset or no of images , if you replace -1 with 42000(no of rows of train dataset) then also it will work fine . The above code will display the below image

Hand written images

Voila !!! now we have seen how the image looks like lets pass this dataset through a K-Nearest Neighbour Classifier which will give an accuracy of 96% .