Coefficient and intercept in linear regression

Coefficient and intercept in linear regression play an important role in machine learning. For the beginners, this concept is essential.

Why? Let’s try to understand the topic in this section.

Firstly, let’s imagine an equation like below.

y = mx + b

In the above equation, y is the dependent variable, and x is the independent variable.

On a graph paper, we can plot these two variables quite easily.

The variable x represents the X axis and y represents the Y axis.

We have seen in our previous section, if the value of y has increased with the increase in value in x, we call it a positive relationship.

However, the opposite is a negative relationship.

In machine learning terms we call the y or dependent variable an outcome. 

On the other hand, the independent variable is known as predator.

And in the above equation, ‘b’ is a constant value or intercept. 

As we plot them on a graph paper, and change the value of x, the value of y also changes. Right? 

As a result, we get a line drawn on the graph.

When there is a line, there must be a slope.

In the above equation, ‘m’ is the slope of the line, or in other terms, regression coefficient.

Let’s get some code first and import the necessary libraries like Pandas, scikit-learn, Matplotlib, and NumPy.

import pandas as pd
import numpy as np
from sklearn import datasets, linear_model, model_selection
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

url = 'https://raw.githubusercontent.com/sanjibsinha/Machine-Learning-Primer/main/newpetals%20-%20Sheet1.csv'
data_frame = pd.read_csv(url)
data_frame

Running the code in Google Colab gives us the data table.


Data table with a negative relationship
Data table with a negative relationship

We can take the valus of length and width separately.

Plotting them is quite easy.

y = data_frame.width
x = data_frame.height
     

ax = plt.axes()
ax.scatter(x, y)

It gives us the follwing output.


Plotting the negative relationship
Plotting the negative relationship

Now we can train the model with the help of NumPy and get the correct prediction line with the help of Matplotlib.

Next we will use the NumPy polyfit() function and pass the degree as 1.

(m, b) = np.polyfit(x, y, 1)
yp = np.polyval([m, b], x)
plt.plot(x, yp)
plt.grid(True)
plt.scatter(x,y)

It gives us the following output.


The model shows the prediction line
The model shows the prediction line

How to get the values of coefficient and intercept

When the degree is 1, we can get the value of m and b also.

That means we get the value of the regression coefficient and intercept.

(m, b) = np.polyfit(x, y, 1)
print(m, b)

# output
-1.1844342707652071 9.946566383257037

A small test will prove whether our model predicts in the right direction or not.

Remember, the plotting line displays the negative relationship.

Therefore, if we provide a high value of the length, our model will predict a diminished-value of the width. Right? 

Since the equation is y = mx + b, we can provide the value of m, x, and b. It will get us the value of y.

y = -1.18443427*9.9 + 9.946566383257029

print(y)

# output
-1.779332889742971

We can check the same value with the scikit-learn library. 

lin_reg = linear_model.LinearRegression()
lin_reg.fit(data_frame.drop('width', axis='columns'), data_frame.width)
lin_reg.predict([[9.9]])

# output
array([-1.7793329])

In the next section we will discuss the same concept but with the help of multiple variables.

In that case, the value will depend on the values of multiple independent variables.

For more such machine learning primer code, 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

One response to “Coefficient and intercept in linear regression”

  1. […] However, before starting the discussion, we must go back and recollect to the previous section where we have discussed linear regression in detail. […]

Leave a Reply