Jean de Dieu Nyandwi

Jean de Dieu Nyandwi

A Simple Motivation To Machine Learning From High School Problem Sets!

A Simple Motivation To Machine Learning From High School Problem Sets!

In this simple article, I reflect back on my early days of high school and how machine learning could have solved some hard problems back then!

Subscribe to my newsletter and never miss my upcoming articles

I remember back in the early days of high school, we used to have maths problems where we could be given a table of two variables, X and y, and the question was to find the linear relationship (equation) between X and y.

Here is how the table used to look like:

Screen Shot 2021-06-18 at 11.21.16.png Image by author: Our sample table

At the time, we had to crunch numbers until we get that the relationship between X and y is y=2X+1. This is yes a simple task (if you look closely in the table above you would find out immediately) but as you can also imagine, this was not a trivial question for an average student. After the equation was found, find y(9) was a straight thing. y(9) = (2 * 9) + 1 = 19.

I used this example to make the point for the article that we somehow did Machine Learning before we knew it. Or maybe we acted as machines :)

After all, machine learning is a technique of solving problems without having to explicitly program the machine where we give it the input data (X) and the results (y) and we can get the rules that link the input data and the results. In the context of our scenario, the rules would be the equation y=2X+1.

So, How can Machine Learning solve it?

We will use a classical machine learning library called Scikit-Learn, NumPy for creating our sample data, and Matplotlib for plotting the data.

Let's imports them.

from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt

After we have imported libraries, we will create our data. It's a NumPy array, X and y. If you are too quick to crunch the numbers, you are going to see that the Xand y represent a linear equation y=2X+1.

X = np.array([[1.0],[2.0],[3.0],[4.0],[5.0],[6.0],[7.0],[8.0]], dtype='float')
y = np.array([[3.0],[5.0],[7.0],[9.0],[11.0],[13.0],[15.0],[17.0]], dtype='float')

If you plot X and y, you're going to see that it is the line.

plt.plot(X,y)
plt.annotate('y=2X+1', xy=(2,4))

Plotting Linear data.png Image by author: Plotting sample data

Now that we have the data, it is time to create the machine learning model that we can use to find y(9). But we will have to teach it first (or to train/fit it on the data). By using the fit method, we are going to pass the input data X and output data y to the model we defined. y is also referred to as label.

model =  LinearRegression()
model.fit(X, y)

Hum, that was quick. Now the model is trained on the data, so that means we can predict the value of y(9). It should be 19 or close. Let's see that!

print(model.predict([[9.0]]))
Output : [[19.]]

As you can see, it did well. It was able to learn the relationship between X and y just from the data. This is also what it means when we say that different to traditional programming which requires rules and data to give results, machine learning takes data and results and give the rules. Let's try to see the rules in our example.

print(model.coef_)
Output: [[2.]]
print(model.intercept_)
Output: [[1.]]

Great, the model was able to determine the exact components of our linear equation.

The coef or coefficient is commonly known as weight. So in this case it is 2, and it is multiplied by the input data X. On the other hand, 1 is the intercept, commonly known as bias. Combining them we get our equation, y=2X+1. These two parameters (weights and biases) are the two output components or parameters of any machine learning model.

In this case, since the data was so simple, it is easy to directly tell that the output is a linear equation, but when it comes to real-world scenarios, it may be hard because you have many features and data points.

If you have done things like that too in your high school years, you are probably intrigued by how cool machine learning is!

To be honest, this is in a fraction of what drew me to machine learning too, the fact that we can use it to solve the problems that we deal with in real life. When I first learned machine learning on real-world datasets, it didn't sink in particularly because I was using datasets like breast cancer when I had no idea of how cancer is diagnosed, or even cat & dog classifier when I could recognize cats easily (but now I know that it's not easy for machines to make sense of images). But when I thought about that simple scenario of a line equation, I got motivations to do more!

Hopefully, this can motivate you too or perhaps your son/daughter/nephew(to list)!! And here is the code (along with full texts) if you would like to try it!

Thank you for reading!


I actively share content around ML ideas, tips and best practices on Twitter. If you would like to connect, you're welcome. Every day, I share one or two things that you will find insightful.

 
Share this