What is Multiple regression?

Regression

Regression is one one of the most common phenomenas of Machine Learning – it is used to identify and define the relationship between the variables. Regression is famous for predicting a future outcome by first training the model on a given data, and then making predictions based on training data.

Linear regression

Linear regression uses the relationship between data points to draw a straight line through all of them. This line can then be used to predict future values.

Linear Regression uses a line plot between the data values to identify and define the relationship between two variablesthe dependent variable (Y) and independent variable X. It is then used to model the relationship between X and Y. This line plot is further used to predict a value Y based on value X.

Multiple regression

Multiple regression is similar to linear regression. The only difference is that, now, we can have more than one independent value. This means that the predictions can be made with two or more variables.

Y = B + B1*X1 + B2*X2 +....+ ∈
where,
// B is the intercept value
// B1 is the co efficient of first independent value
// B2 is the co efficient of second independent value

Code

Consider you have the following dataset of cereals:

import pandas as pd
df = pd.DataFrame({"name" :["100% Bran","Almond Delight","Bran Flakes","CapnCrunch","Cocoa Puffs","Nut&Honey Crunch","Quaker Oatmeal","Corn Pops","Cream of Wheat","Golden Crisp"],
"calories" : [70, 110, 90, 120, 110, 120, 100, 110, 100, 80],
"shelf" :[3,3,3,2,2,2,1,2,1,1],
"rating" :[68,34,53,18,50,50,52,70,80,65 ]}) 

We can predict the rating of cereal based on its calories, but with multiple regression, we can use more variables, such as shelf, to make the predictions more accurate:

X = df[['shelf', 'calories']]
y = df['rating']

We will use some methods from the sklearn module for linear regression:

M = linear_model.LinearRegression()
// fit to train the model on data
M.fit(X, y)

Now, we can make predictions:

// predict the rating of a cereal when the  shelf life is 2 and calories are 85
pred = M.predict([[2, 85]])
import pandas as pd
from sklearn.linear_model import LinearRegression
df = pd.DataFrame({"name" :["100% Bran","Almond Delight","Bran Flakes","CapnCrunch","Cocoa Puffs","Nut&Honey Crunch","Quaker Oatmeal","Corn Pops","Cream of Wheat","Golden Crisp"],
"calories" : [70, 110, 90, 120, 110, 120, 100, 110, 100, 80],
"shelf" :[3,3,3,2,2,2,1,2,1,1],
"rating" :[68,34,53,18,50,50,52,70,80,65 ]})
X = df[['shelf', 'calories']]
y = df['rating']
M = LinearRegression()
M.fit(X, y)
pred = M.predict([[2, 85]])
# pred contains the predicted value for these two given parameters
print(pred)
Copyright ©2024 Educative, Inc. All rights reserved