Multiple Linear Regression
In our previous discussion, we covered Simple Linear Regression, where one independent variable (X) is used to predict a dependent variable (Y). But what happens when the result depends on more than one factor? This is where Multiple Linear Regression (MLR) comes into the picture.
Table of Contents

Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
What is Multiple Linear Regression?
Multiple Linear Regression is an extended form of Simple Linear Regression. It is used to model the linear relationship between a single continuous dependent variable and two or more independent variables. These independent variables can be continuous or categorical.
Real-Life Example
Suppose you want to predict the CO emissions of a car. Looking only at the engine size may not be enough because other factors, such as the number of cylinders, can also affect emissions. This is a classic example of where Multiple Linear Regression can be used.
Key Points About MLR
- The dependent variable (Y) must be continuous.
- The independent variables (X) can be continuous or categorical.
- Each independent variable should have a linear relationship with the dependent variable.
- The regression line is fitted through a multidimensional space.
MLR Mathematical Equation
The Multiple Linear Regression model can be represented as:
Y = b₀ + b₁x₁ + b₂x₂ + b₃x₃ + … + bₙxₙ
Where:
- Y = Target variable
- b₀ = Intercept
- b₁, b₂, …, bₙ = Coefficients
- x₁, x₂, …, xₙ = Independent variables
Assumptions in MLR
- There should be a linear relationship between the dependent and independent variables.
- Residuals (errors) should be normally distributed.
- There should be no or minimal multicollinearity between independent variables.
Implementing Multiple Linear Regression in Python
Let’s look at a practical example where we use Python to predict company profits.
Problem Statement
We have a dataset containing information about 50 startup companies. The dataset includes:
- R&D Spend
- Administration Spend
- Marketing Spend
- State
- Profit (Target Variable)
Step 1: Data Pre-processing
# Importing necessary libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing dataset
dataset = pd.read_csv('50_CompList.csv')
# Extracting independent (X) and dependent (Y) variables
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 4].values
Step 2: Encoding Categorical Data
The State column contains categorical data, so we encode it using LabelEncoder and OneHotEncoder.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
labelencoder = LabelEncoder()
x[:, 3] = labelencoder.fit_transform(x[:, 3])
# Creating dummy variables
ct = ColumnTransformer(
[("State", OneHotEncoder(), [3])],
remainder='passthrough'
)
x = ct.fit_transform(x)
# Avoiding the Dummy Variable Trap
x = x[:, 1:]
Step 3: Splitting the Dataset
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.2, random_state=0
)
Note: Feature Scaling is not required here as the library handles it internally.
Step 4: Fitting the MLR Model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)
Step 5: Predicting the Results
y_pred = regressor.predict(x_test)
# Comparing predictions with actual results
comparison = pd.DataFrame({
'Actual': y_test,
'Predicted': y_pred
})
print(comparison)
Output
You will get a table comparing the predicted profits with the actual profits from the test set. This helps you understand how well the model performs.
Download New Real Time Projects :- Click here
Final Thoughts
Multiple Linear Regression is one of the widely used techniques in machine learning and statistics. It is simple yet powerful when multiple factors are involved in making a prediction.
By mastering MLR:
- You can make better business predictions.
- Understand how different features affect the output.
- Develop more accurate forecasting models.
multiple linear regression formula
multiple linear regression example
multiple linear regression python