<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Christian Rápalo]]></title><description><![CDATA[Hello! I'm a computer science engineer, with a passion for machine learning.]]></description><link>https://hashnode.christianrapalo.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1631245842025/xRJ8yjuFt.png</url><title>Christian Rápalo</title><link>https://hashnode.christianrapalo.com</link></image><generator>RSS for Node</generator><lastBuildDate>Sun, 10 May 2026 10:10:28 GMT</lastBuildDate><atom:link href="https://hashnode.christianrapalo.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Preparing the data]]></title><description><![CDATA[We are going to create a web app that can classify digits from 0 to 9.To achieve this, we are going to train a machine learning model with the mnist dataset, which contains 70,000 black and white images of handwritten digits in total. Each image is 2...]]></description><link>https://hashnode.christianrapalo.com/preparing-the-data</link><guid isPermaLink="true">https://hashnode.christianrapalo.com/preparing-the-data</guid><category><![CDATA[Python]]></category><category><![CDATA[TensorFlow]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[hashnodebootcamp]]></category><dc:creator><![CDATA[Christian Rápalo]]></dc:creator><pubDate>Fri, 17 Sep 2021 05:44:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1631856714558/WETyNZ5TX.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We are going to create a web app that can classify digits from 0 to 9.<br />To achieve this, we are going to train a machine learning model with the mnist dataset, which contains 70,000 black and white images of handwritten digits in total. Each image is 28 x 28.<br />Also, we are going to create a web app where we can draw a digit, and will use our model to predict the number.</p>
<p>This is the first part of the series, where we are going to set up the environment, get the data, normalize it, and plot some numbers.</p>
<h1 id="tools-well-be-using">Tools we'll be using</h1>
<p>We will be using a set of tools and libraries that will make it easier to create machine learning models and plot the results.</p>
<p>These tools are: </p>
<ul>
<li>Google Colab</li>
<li>Tensorflow</li>
<li>NumPy</li>
<li>Matplotlib</li>
</ul>
<h1 id="setting-up-the-environment">Setting up the environment</h1>
<p>We will be using Google Colab, which is a Jupyter-like Python environment that lets you create notebooks and execute python code on the cloud.</p>
<p>To get started with Google Colab you’ll only need a Google account, and go to https://colab.research.google.com/</p>
<p>Here it will prompt this modal: </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631819920201/p1NQ9zrU3.jpeg" alt="1. Modal_Colab_Google.jpg" /></p>
<p>And to create a new notebook you need to click on the “New Notebook” button.</p>
<p>This will create a new environment to execute your code.</p>
<h1 id="geting-the-data">Geting the data</h1>
<p>As mentioned before, we will be creating a mnist classifier.<br />Lucky for us, mnist is a popular dataset that has been used a lot before to try new machine learning models, so the team developing Tensorflow has included that dataset into Tensorflow, and that makes it easy to fetch it.</p>
<p>First, we need to import all the libraries we will be using.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">from</span> tensorflow.keras.models <span class="hljs-keyword">import</span> Sequential
<span class="hljs-keyword">from</span> tensorflow.keras.layers <span class="hljs-keyword">import</span> Dense, Flatten, Dropout
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np 
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
</code></pre>
<p>As you can see, we imported a lot of things which I’ll be explaining right now:</p>
<p><strong>Tensorflow</strong>: is the library that contains all we need to create deep learning models.</p>
<p><strong>NumPy</strong>: is a numerical library that lets us execute mathematical operations in arrays and matrices.</p>
<p><strong>Matplotlib</strong>: is a plotting library for making amazing charts.</p>
<p>After importing all the libraries, we need to fetch the dataset.<br />If you import the mnist dataset from Tensorflow, it's already divided into 2 different sets for us, 60,000 images for the training set, and 10,000 images for the test set.<br />Before building a model, it’s always good to make this division of training data and testing data, because it will let us verify if our model was trained correctly, or if it’s overfitted or underfitted. 
Sometimes you will see a third set which is a validation set, but we will not do that here.</p>
<p>To import the two sets of data we do: </p>
<pre><code class="lang-python">mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
</code></pre>
<p>Now that we have the data, we have to normalize the images.<br />A black and white image is a matrix of pixels with values from 0 to 255 (being 0 black and 255 white). As you can see, there’s a long gap between the two values. We want to reduce this gap without losing too much information about the image.<br />A popular way to do this is by dividing each pixel by 255. This is because if we do that, we will get a matrix of values from 0 to 1. Because 0/255 is equal to 0 and 255/255 is equal to 1.<br />Normalizing the images will reduce the training time and normally will improve the accuracy.</p>
<p>To do this we do:</p>
<pre><code class="lang-python">x_train, x_test = x_train / <span class="hljs-number">255</span>, x_test / <span class="hljs-number">255</span>
</code></pre>
<h1 id="analyzing-the-data">Analyzing the data</h1>
<p>For other machine learning problems, you will probably need to deeply analyze the data and fix it, by this I mean to plot a distribution chart to see if every possible output is balanced, see if there are null or empty values in the dataset, etc...<br />But because mnist is a popular dataset, it has already been analyzed and fixed, so we don’t need to do too much here.</p>
<p>But what we will do is plot an image of the training set to see a little bit how they are, and also print the dimensions of the data because we will need this later.</p>
<p>To plot an image, we will use Matplotlib which we have already imported, and execute this:</p>
<pre><code class="lang-python">index = <span class="hljs-number">5</span>
plt.imshow(x_train[index])
plt.axis(<span class="hljs-string">'off'</span>)
plt.title(<span class="hljs-string">f'Real Value: <span class="hljs-subst">{y_train[index]}</span>'</span>)
</code></pre>
<p>The result we will be getting is something like this: </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631824840112/SnyazfNtyK.png" alt="2. Wrong_Plot.png" /></p>
<p>As mentioned before, mnist is a set of black and white handwritten images, but the plot shows some colors.<br />That’s because when plotting, we need to specify that our dataset is grayscale.</p>
<pre><code class="lang-python">index = <span class="hljs-number">5</span>
plt.imshow(x_train[index], cmap=<span class="hljs-string">'gray'</span>)
plt.axis(<span class="hljs-string">'off'</span>)
plt.title(<span class="hljs-string">f'Real Value: <span class="hljs-subst">{y_train[index]}</span>'</span>)
</code></pre>
<p>Now you can see it’s plotting the image in the right way:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631824915082/LKBLsRdhW.png" alt="3. Correct_Plot.png" /></p>
<p>You can change the displayed image by changing the index.</p>
<p>Now what we need to know is the dimension of the dataset. By this, I mean how many images do we have, and what is the size of those images (I have already told you the dimensions, but we will imagine that we don’t know that).</p>
<p>We can print this by doing:</p>
<pre><code class="lang-python">print(x_train.shape)
</code></pre>
<p>Result: <code>(60000, 28, 28)</code></p>
<p>With that, we can know that we have 60,000 training images with a size of 28 x 28 each.</p>
<h1 id="congrats">Congrats!</h1>
<p>Hopefully you have learned a few things with this first part.<br />Knowing the basics will help you at the moment when you start working with custom datasets.<br />What I want you to remember is to always divide your data into 2 sets and preprocess it before starting to build the model.<br />If your data is “garbage”, your model will also be “garbage”, so it’s important to spend quite some time preprocessing it.<br />As I said before, here we didn’t do too much preprocessing because the mnist was created to try new models without spending too much time working with the data, the only thing we did was normalize the images.</p>
<p>In the second part, we will build and train the model.</p>
]]></content:encoded></item></channel></rss>