Linear Regression and Machine Learning Algorithm

Linear Regression refers to many points. It’s one of the core machine learning algorithms. Also, it’s the easiest of all.

As a result, it makes predictions for continuous and real numerical values, such as sales.

At the same time, it is a core statistical and data science concept.

Why?

Because in statistics, or in data science we have learned what is Mean, Median and Mode.

Right?

In linear regression there are two variables. One is a dependent variable and the other is the independent variable.  

As an outcome, the value of the dependent variable always depends on the independent variable. 

We have seen the same thing in a mathematical function as follows.

Y = f(X) + b

According to the equation, the value of “y” depends on the value of “x”. 

Therefore the function always returns one value.

Remember any python function. It also returns one value.

Right?

But what is the function of “b”?

In vector algebra, we can plot such functions on the Cartesian Coordinate system.

As a result, if we think that x represents the x axis and the y represents the y axis, we should start the graph from one point.

For that reason, “b” is the point where the y axis intercepts the x axis.

Let’s consider a simple function as follows.

y = f(x) + 2
x = 2

# output:
y = 4

Let’s try to draw the graph.

Plotting a simple function
Plotting a simple function

For example, now let’s consider that there are several such points. Although they scatter at different places. 

Therefore, we’ll see different values of y and x.

Moreover, each time, the value of y will change depending on the value of x.

That means y is the dependent value and x is the independent value.

How Linear Regression works

That’s the question which we want to address now.

If we see that the value of y increases depending on the value of x, then we will try to predict the outcome.

That is the linear regression approach of the machine learning algorithm.

Let’s plot different data points first.

import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 2.5, 3, 4, 6, 9, 11, 12]
y = [1, 3.5, 4.5, 5, 7, 9, 10, 13, 14]
plt.plot(x, y, 'ro')
plt.axis([0, 15, 0, 15])

We see clearly the value of y increases with the value of x. Although the increases are arbitrary. Not regular that we have seen in the above function.

As an outcome, we see different data points in red.

Linear regression first example
Linear regression first example

If we look at the values of x and y, we can see a pattern.

In most cases, as the value of x increases, the value of y also increases.

However, it won’t happen all the time. 

In that case, we will see the red dots in the negative regions.

But for a simple example of linear regression model, we make it look like this.

Now let’s use Matplotlib and watch the prediction. As an outcome, it becomes one of the simplest machine learning algorithms.

plt.plot(x, y, 'ro')
plt.axis([0, 15, 0, 15])
plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)))
plt.show()

Linear regression prediction
Linear regression prediction

As we progress, we’ll find more such examples.

Therefore, stay tuned.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

One response to “Linear Regression and Machine Learning Algorithm”

  1. […] clearly indicates the prediction values. As the variable “b” depends on the value of the variable “a”, we see how the variable […]

Leave a Reply