Multiple Regression in scikit-learn

The LinearRegression method in sckikit-learn can handle multiple independent variables to perform multiple linear regression. Follow this tutorial which combines your knowledge of pandas with scikit-learn.

Regression

Regression is one of the most common phenomena in 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 variables. 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)


Source: Sarvech Qadir, https://www.educative.io/answers/what-is-multiple-regression
Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 License.

Last modified: Wednesday, September 28, 2022, 12:07 PM