How do you predict a single value in linear regression?

How to predict a single value in linear regression using a machine learning model? Let’s try to understand with a simple example.

In our previous section we have discussed how we can use the linear regression method of the scikit-learn library to make it happen.

However, to do this operation we also need another great library that we use pretty often in building the machine learning algorithms. 

Yes, we’re talking about the Pandas.

To predict a single value in linear regression we need to understand the mathematical formula of linear algebra first.

Here the value of the dependent variable changes with the change in the value of the independent variable.

When the value of an independent variable increases, and it also increases the value of the dependent variable, the relationship is positive.

And if it reverses, the relationship is negative.

Now our prediction will depend on the same relationship.



If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.
But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


For example, what kind of data we have, decides the prediction.

Let’s see the table first.


Data table with two variables
Data table with two variables

Firstly, the dataset shows that the relationship is positive.

Here the length is the independent variable, and the value of width depends on the value of length.

Therefore the mathematical equation looks like below.

y = mx + b

In the above equation, ‘m’ is the coefficient and the ‘b’ is the intercept of the line based on the values of x and y.

Let’s import the essential libraries first.

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


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

Before showing the dataframe, we need to keep our ‘csv’ file somewhere.

To practice, you can also download the same file from my GitHub repository.

Now we can plot the variables with Matplotlib. That is another essential library for machine learning to visualize data.



plt.xlabel('length')
plt.ylabel('width')
plt.scatter(data_frame.length, data_frame.width, color='blue', marker='*')

It allows us to visualize data.

Let’s get a look.


Plotting a single value in linear regression
Plotting a single value in linear regression

In the above image, the x axis represents the independent variable and the y axis portrays the dependent variable.

Next, we will use the linear regression method of the scikit-learn library so that we can pass any length-value to get the exact prediction of the width.

Remember we have trained our machine learning model on the positive relationship based on linear regression. 

# now we drop width

X = data_frame.drop('width', axis='columns')

# getting only width
y = data_frame.width

# We're creating linear regression object that will be best fit for prediction
linear_regression_object = linear_model.LinearRegression()
linear_regression_object.fit(X, y)
     
# based on this linear_regression_object we can predict any width from a length

linear_regression_object.predict([[6.3]])

We have passed a value of the length and as a result we have got this width as below.

array([2.6725429])

We can easily test whether our prediction is correct or not.

In the next section we will see to that and many more.

So stay tuned, be happy and cheer up.

For more basic machine learning algorithms, please visit the respective branch of GitHub Repositoy.

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 “How do you predict a single value in linear regression?”

  1. […] Remember the two variable linear regression equations we have discussed before. […]

Leave a Reply