# How It Is Done: Machine Learning

**5.**

That’s the number of Terminator films that convinced the world that ramping up our machines’ intelligence would mean the end of the world.

But all these years later, our machines are smarter than ever and learning at exponential rates. Our machines’ learning capabilities have drastically improved our lives in all facets from healthcare to security.

**So if a machine’s ability to learn is ushering in the golden age of human life, you should understand how it learns.**

Working at a pet shop, my boss has asked me to help him organize dogs and cats. Me being efficient, I wondered if I could build a system that could identify what is a dog or a cat on its own **using machine learning**. Let’s build this system.

*Note: the machine learning we will be creating is supervised learning. If you want to learn more about other types of machine learning, click **here**.*

**Identifying Factors**

Before we can start building our **model**, the system that answers if this animal is a dog or cat, we first have to choose what data we will collect to train our model. **Training **is how we create an accurate model that answers our questions correctly.

For simplicity, we will choose **two** characteristics of dogs and cats as our data, (1) snout length and (2) eye roundness.

Now that we have decided our data, let’s start **Step 1 of 6** in the process of machine learning.

# Collecting Data

The first step of machine learning, collecting data, is arguably the most important step of the process. Without data, we lose the basis of our model and have no model to train. So it becomes extremely important that the amount of data we collect and the quality of that data be a good representation of dogs and cats so we can answer the question, “is this a dog or a cat?”

After collecting data from the dogs and cats in the pet store, I put some sample data into this table for you to see. We have now completed Step 1 of machine learning process, let’s move on to Step 2.

# Preparing The Data

**After all our data is collected, we have to prepare it to use in our training.**

Preparation begins with ensuring that there are an equal amount of data entry points for both dogs and cats. For example, if they are 100 data points for dogs and only 20 for cats, a bias will exist in the system as it will think that the pet is a dog most all the time. So let’s keep our learning process as neutral as possible to give us the most accurate answers.

Next, we need to randomize our data’s order. We don’t want the order of our data to affect what the machine learns, since that’s not part of determining whether the pet is a dog or a cat.

Finally, we will need to split our data in two parts. The first part will be used to train our model. The second part will be used for evaluating our trained model’s performance. I won’t use the same training data for evaluation because our system could just memorize the data and already “know” the answers to which pet is a dog or a cat.

**Now that our data is prepared, let’s figure out the model to show our data in Step 3 of the learning process.**

# Modeling

Our animal data has been collected and prepared correctly so now we have to choose a model that our data will be trained on. While there are tons of different, complex models we can use to show our data, we will keep it simple. We will use a linear model that takes our 2 identifying characteristics as inputs.

This is an example of a basic linear model. The line in between is the regression, determines the strength of the relationship between variables, and each of the dots are data points.

**Let’s create our linear regression by training our model to distinguish dogs from cats in Step 4.**

# Training

Now we will start the bulk of the learning process, training. We will use the data we collected on dogs and cats to improve our model’s ability to predict the pet.

Think of training our system like me learning to play basketball. At first, I wasn’t able to dribble, shoot and pass. However, after lots of practice and learning for my mistakes, I became a competent basketball player. After years of playing and learning, I could play in the NBA (okay maybe this isn’t likely but you get the point, practice makes perfect).

We will do similar training on a much smaller scale with our system to identify dogs and cats.

**We will use our linear model and the formula for this model is y=m*x+b.** “m” is the slope of that line, “x” is the input, “b” is the y-intercept, and “y” is the overall value of the line at the input of “x.” The values that we will train are “m” and “b” as there is no other way to change the position of the line with our input of “x” or output of “y.”

To train our “m” and “b” values, we will use **matrices** (plural for a **matrix)**. A matrix is simply a rectangular array of numbers, expressions or symbols. Look below for an example.

Now that we have a basic understanding of what a matrix is, let’s apply to our system. Throughout the machine learning process, there are many “m’s” since there may be many features or identifying characteristics. The collection of these “m” values is usually formed into a matrix, that we will denote **“W”**, for the **“weights” matrix**. Similarly for b, we arrange them together and call that the **bias matrix**.

This is a snippet of how we would arrange our weights and biases in matrices for training.

Our training process involves putting in some random values for W and b, and attempting to predict the output with those values. Initially, our system will do poorly, just like me trying to shoot a basketball for the first time. But we can compare our model’s predictions with the correct output, and adjust the weight and bias values to increase correct predictions.

When we first start our training, it’s like we are throwing a random line through the data. Then as each iteration of the training progresses, the line moves, inch by inch, closer to a clear distinction between dogs and cats. So, by repeating this training process, we move closer towards getting as many correct predictions as possible.

**After many iterations, I can say I am happy with our model’s ability to predict correctly which animal is which. Now let’s see if our model works with data it wasn’t trained on in Step 5.**

# Evaluating and Tuning

Once the training of the model is complete, we need to see if the model actually works by **evaluating**. This is where the second dataset we created during preparation comes into play. This dataset was not used to train our model so we will test our model against data that it has not yet seen. Evaluating is crucial in the machine learning process as it gives us a better understanding of how our model could perform in real application.

While evaluating, we can **tune** our parameters to improve our training process. We can tune our parameters by showing our model more data to improve accuracy of predictions.

We can also tune the learning rate during training. Learning rate refers to how far we shift the line during each iteration. This rate plays a key role in how accurate our model can become, and how long the training takes. So, we can make sure we tune our learning rate to ensure optimal results.

**Now that we are happy with the evaluation and tuning of our model, we can finally put it to use in Step 6!**

# Inference

Finally the last step of machine learning. Inferring, or predicting, is where our system actually answers the question. Using the model we trained, our system predicts whether the pet is a dog or a cat, based on the parameters given (snout length and pupil roundness).

After getting a new pet in our store, we record the data needed for our system to determine if this animal is a dog or cat. Once our data is inputed, our system uses the trained linear model to predict that this pet is a dog. In this case, our system is correct as this data came from a dog! Now I can rest assured that our system can do my job for me. Aside from me having to give our system the data needed to answer what animal this is… **Nevertheless, we have completed the machine learning process!**

# Key Takeaways

- The capabilities of machine learning are limitless. While we created a simple model to determine if a pet was a dog or a cat, real-life applications are changing the way sectors like healthcare, production, and security operate.
- The 6 steps of machine learning are:

**This machine learning process can be applied anywhere, so let’s apply it to the real-life application of identifying skin cancer.**

Step 1: We would collect data on different types of skin/moles (both cancerous and non-cancerous) and we can identify differences by colour and radius.

Step 2: Let’s prepare our data by making sure we have the equal number of data points for both cancerous and non-cancerous skin.

Step 3: We model our data by using a polynomial, 5th degree regression (we’re getting complex to show you the endless number of model types that can be used)

Step 4: We train our model by inputting weights and biases and iterating, much like we did with our dogs and cats.

Step 5: Just to double check that our model is primed for use in clinics, we evaluate our model on new data.

Step 6: Our model is ready for use and will detect if skin is cancerous or not!

**What we have essentially mapped out is this:**

# Try out some machine learning!

Still interested in learning more about machine learning? Here are two resources you can use to try out some of your own machine learning:

**For those new to machine learning, ****Teachable Machine**

Teachable Machine is a very easy way to teach a machine using your camera. I’d highly recommend checking it out if you’re getting into machine learning!

**For those more comfortable with machine learning, ****TensorFlow Playground**

Here you can play with training and parameters, and run training against mock datasets.

# I hope you enjoyed this article, before you go:

- Clap this post if you learned something new.
- Share with family and friends who would be interested!
- Subscribe to my Medium and connect with me on LinkedIn to stay updated with my venture in various technologies!