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!




Leveraging machine/deep learning and image processing in medical image analysis | https://abder.me

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How and why did White Colonists ‘Stuff Up’ the Better Ideas of Black Mathematics Teachers so Badly?

Two perceptions of maths

Could Spider-Man Catch MJ in the No Way Home Trailer?

What are the next Pythagorean Triangles’ sides after 3,4,5 ? how about the next, next one?

What is Escape Velocity?

The Mathematical Breakdown of Road Rage

Why Did Lewis Carroll Write a Play About Geometry?

A black and white photograph of many different theatrical masks on poles.

Get the Medium app

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
Dr. Abder-Rahman Ali

Dr. Abder-Rahman Ali

Leveraging machine/deep learning and image processing in medical image analysis | https://abder.me

More from Medium

What Is Machine Learning Overfitting And How Can It Be Avoided?

How to Master TensorFlow Variables in 6 Easy Steps

Grooming The Booming Data

Breast Cancer Classification: Using Logistic Regression