A Smooth Introduction to Linear Regression and its Implementation in PyTorch (Part-I)
The term linear regression might seem a bit complex at first, but don’t worry, it is a simple but powerful concept. Let us go through an example to understand the concept better.
I will throw some random data here and see what happens. Say that we have two variables h and r, that refer to the time of the day and how many pages a person read, respectively.
Let’s assume that the person starts reading at 9:00 AM and finishes at 2:00 PM. We will denote the hours as hour 0 for 9:00 AM, hour 2 for 10:00 AM, and so forth:
The table below will now show the time of the day h and how many pages the person read r in each hour.
This is how the data looks like in an h-r plane:
So, for instance at time 1 (10 AM) the person read 12 pages.
Now, what we would like to do is find the linear relationship between the two variables h and r. This can be done by finding the equation of the line that best fits the points (h,r). That is, an equation that would provide us with a line that is closest to all points in the h-r plane. This is the main concept of linear regression!
We refer to the distances of the points from the line as deviations. Finding a best fit means minimizing the sum of the squares of distances (deviations).
I will now list some formulas we will be using to find the equation of the best fit line.
Let’s now go ahead and substitute our values (h and r) in the aforementioned formulas.
Awesome! Didn’t I tell you it is a simple yet powerful method?
This was a simple walkthrough of how linear regression works and how it finds our best fit line. I know the example might have been more realistic if we set the pages to increase incrementally along the hours, but I think you got the main idea at least, which is the main goal of this post.
In the second part of this tutorial I will be showing you how we can implement the above example in PyTorch!