Digit Recognizer - Introduction to Kaggle Competitions

Solving MNIST Digit Recognition Task (0.995)

In today’s article, I am going to show you how to master machine learning skills by participating in Kaggle data science competitions. We will solve a simple written digits recognition task (MNIST) and compare our results with others’. You will be challenged to master your analytical and coding skills in order to succeed in a highly competitive Kaggle community.

What is Kaggle?

Kaggle is an online community of data scientists and machine learners, owned by Google, Inc. Kaggle allows users to find and publish data sets, explore and build models in a web-based data-science environment, work with other data scientists and machine learning engineers, and enter competitions to solve data science challenges. Kaggle got its start by offering machine learning competitions and now also offers a public data platform, a cloud-based workbench for data science, and short form AI education.

Kaggle offers 5 main functionalities i.e Competitions, Datasets, Kernels, Discussion and Learn.

Feel free to check all of them but in this article, we will focus only on the Competitions.


Probably the most attractive and popular part of Kaggle, especially for the competitive ones who are motivated by rivalry with others.

Competitions usually follow the same pattern.

  1. Given a problem and a corresponding dataset.
  2. Create a solution that can efficiently generalize and solve a problem on a new data that weren’t explicitly seen by the model.
  3. Evaluate a solution and compete with others.
  4. Win knowledge or $.

To get a better understanding of how competitions work, let’s take part in one of them!

Digit Recognizer

About the Competition

Start here if you have some experience with R or Python and machine learning basics, but you’re new to computer vision. This competition is the perfect introduction to techniques like neural networks using a classic dataset including pre-extracted features.

tl;dr MNIST digit recognition is a Computer Vision’s Hello World 🙂.

Competition Details

Dataset: MNIST Handwritten digits

Description: Classification of handwritten digits, 10 classes (0–9). Given a dataset of labeled handwritten images, build a classifier that would assign correct labels to the new (not seen by the model) images.

Training: 42k labeled images

Testing: 28k unlabeled images


Our goal is very straightforward and clear so let’s get straight to the building of the classifier.

You can fork my code directly on Kaggle

or as usual on GitHub.

Let’s begin with loading and reshaping data.

Then we can divide our labeled data into training and validation sets. We will let our model learn on the training set and validate its performance with the validation set.

Training: 37.8k (0.9) images

Validation: 4.2k (0.1) images

Our data is almost ready and we could start our training right now but there is one more thing that we could do to improve our classifier - data augmentation.

The idea behind data augmentation is that we can artificially enlarge our training dataset (thus reducing overfitting) by its augmentation.

Take a look at the below example, where from a single image we were able to generate multiple distinct samples using rotation, shifting, zooming etc.

Even though the above examples look very similar to us, computer ‘sees’ each image as a different set of pixels.

This is how it can look in the code, feel free to play with the parameters and used operations.

Now as your data is ready, we can build our Convolutional Neural Net model. If you are new to the CNNs, feel free to check my previous article that covers its basics.

Below CNN model gives about 0.995 accuracy and is a good starting point. Feel free to modify and improve it!

We are good to go, let’s start training!

and monitor the model’s performance for 100 epochs.

Evaluation and Submission

Finally, as our model’s training plots look promising, we can perform a final evaluation and confront our model with the unlabeled testing set.

Above code predicts classes for images from the testing set and outputs the results to the .csv file.

Then we can submit our predictions to the Kaggle’s evaluation system.

We can do it automatically if we are running code on Kaggle

or manually if we are running code outside Kaggle.

Finally, we can see our score on the leaderboard!

And this is how our classifier places us among others.

Not bad! Especially given the fact that our code isn’t heavily tuned/optimized and I encourage you to do so to improve this score.

Moreover, it’s very easy to cheat (but it’s entry level so no one really cares) in this particular competition as the testing set is widely known and available (MNIST) and submissions with 1.0 scores were probably trained on it.

What’s next?

By the end of this article you should, be familiar with Kaggle and its competitions. I encourage you to give it a shot and take part in at least one of them. Kaggle is a great community of helpful and talented machine learning enthusiasts and I invite you to be a part of it!

Questions? Comments? Feel free to leave your feedback in the comments section or contact me directly at https://gsurma.github.io.

And don’t forget to 👏 if you enjoyed this article 🙂.

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store