Regression models in Machine Learning

Regression models in machine learning can help us determine the relationship between variables. In this section we will understand this topic.

Certainly there are different types of regression models in machine learning, but here we will concentrate on linear regression. 

As we just said, we try to find the relationship between two variables. 

In our previous section we have seen how we can plot the Iris data with the head() and tail() method.

Let us take it forward a little bit. 

When we work with the tail() method, it actually gives us the last five datasets where the Iris Virginica flowers show up with their petal width and lengths. 

We can take a look at the table first.

Second Data table form Iris datasets
Second Data table form Iris datasets

By importing the Matplotlib library we can use the scatter method to get a glimpse of data visualization. 

Actually it builds a relationship between sepal width and petal width of the Iris Virginica flower.

From the table it’s evident that the petal width is less and the difference is not much.

As a result, we see the plot has brought them closer. Right? 

Regression in machine learning one
Regression in machine learning one

We can apply the linear regression concept here.

In linear regression, one is a dependent variable and the other is an independent variable. 

When the increase in the independent variable increases the value of the dependent variable, we can say that the relationship is positive.

Here as the sepal width increases, the petal width also increases. So the relationship is positive.

And the plot also gives us the same output.

However, for the head() method, this relationship is quite different. 

Let’s see the code and the data table.

import seaborn as sns
import matplotlib.pyplot as plt
# this will pick up the head part
iris_head = sns.load_dataset('iris')
iris_head.head()

That is very simple. It will load the first five Iris flowers which belong to the category Iris Setosa.

And the fun starts as the petal width is fixed for this variety of Iris flower.

Let’s have a look. 

First Data table form Iris datasets
First Data table form Iris datasets

Of course, in this case, the relationship between two variables will be different. Right? 

Consequently, to plot the relationship we must import Matplotlib.

After that we will use the scatter() method and pass the required parameters.

Here goes the code.

import seaborn as sns
import matplotlib.pyplot as plt
# this will pick up the head part
iris_head = sns.load_dataset('iris')
iris_head.head()
iris_head = iris_head[iris_head['species'].str.contains('setosa', case=True, regex=True)]
plt.scatter('sepal_width','petal_width',data=iris_head)
     

As an outcome, we get this output where the concentration indicates to the value 0.2. Here the Y axis represents the petal width.

Regression in machine learning two
Regression in machine learning two

Now we can plot this with the plot() method and pass the value 0.5 which is minimum in density according to the above image.

How Regression Models work

As a result, the color black is not dark.

On the contrary, it tends to gray.

Regression in machine learning three
Regression in machine learning three

If we change the code, and pass the value 0.2, the image changes.

import seaborn as sns
import matplotlib.pyplot as plt
# this will pick up the head part
iris_head = sns.load_dataset('iris')
iris_head.head()
# iris_head = iris_head[iris_head['species'].str.contains('setosa', case=True, regex=True)]
plt.plot('sepal_width','petal_width', '0.2', data=iris_head)

This time the color of black is the darkest. Why? We know the reason now. The first five data shows that the petal width is fixed. 

Not only can we find the pattern, but we can also say that the Iris Setosa flower has a tendency to have the petal width 0.2.

Regression in machine learning four
Regression in machine learning four

For more machine learning beginner code on various topics, please visit the respective GitHub repository.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

C, C++, Java and Game Development

Twitter

Comments

Leave a Reply