Skip to content
Home » Linear Regression

Linear Regression

1. Definition

Linear Regression is a supervised machine learning algorithm used to predict a continuous numerical value.

In simple language:

Linear Regression finds the relationship between an input variable and an output variable and uses that relationship to predict a new value.

For example:

  • Study hours → Marks
  • Experience → Salary
  • Area of house → Price
  • Advertising expenditure → Sales

2. Basic Idea

Suppose we have the following data:

Hours Studied (X)Marks (Y)
135
245
350
460
570

We can see that generally, as study hours increase, marks also increase.

Linear regression tries to draw a best-fit straight line through these data points and then uses that line to predict marks for a new number of study hours.\(\hat{y} = b_0 + b_1x\)


3. Equation of Linear Regression

The basic equation is:\[ y = mx + c \]

Where:

  • y = predicted output
  • x = input variable
  • m = slope of the line
  • c = intercept

In machine learning notation, it is often written as:\[ \hat{y} = b_0 + b_1x \]

Where:

  • ŷ = predicted value
  • b₀ = intercept
  • b₁ = coefficient/slope
  • x = input feature

Meaning of slope

The slope tells us how much Y changes when X increases by one unit.

For example:\[ y = 10x + 20 \]

Here:

  • Slope = 10
  • Intercept = 20

If x = 5:\[ y = 10(5)+20 \]\[ y = 70 \]

So the predicted value is 70.


4. Best-Fit Line

The main objective of linear regression is to find the best-fit line.

A best-fit line is the line that gives predictions as close as possible to the actual data points.

For example:

Y
│                 •
│              •
│           •
│        •
│     •
│  ─────────────────  Best-fit line
│
└──────────────────── X

The actual data points may not lie exactly on the line.

The difference between an actual value and its predicted value is called the residual/error.\[ Error = Actual\ Value – Predicted\ Value \]


5. Least Squares Method

Linear regression commonly uses the Least Squares Method to find the best-fit line.

The method tries to minimize the sum of squared errors between actual and predicted values.

In simple words:

Choose the line for which the total squared prediction error is as small as possible.

This is why the regression line is also called the least-squares regression line.


6. Simple Linear Regression

When there is:

  • One independent variable (X)
  • One dependent variable (Y)

it is called Simple Linear Regression.

Example

Predict salary based on years of experience:

Experience → Salary

Here:

  • X = Years of experience
  • Y = Salary

Equation:\[ Salary = b_0 + b_1(Experience) \]


7. Multiple Linear Regression

When there are two or more independent variables, it is called Multiple Linear Regression.

For example, house price can depend on:

  • Area
  • Number of bedrooms
  • Location
  • Age of house

The equation can be represented as:\[ y=b_0+b_1x_1+b_2x_2+b_3x_3+\cdots+b_nx_n \]

For example:\[ Price=b_0+b_1(Area)+b_2(Bedrooms)+b_3(Age) \]


8. Example of Linear Regression

Suppose a company wants to predict sales based on advertising expenditure.

Advertising ₹ (X)Sales ₹ (Y)
10,00050,000
20,00070,000
30,00090,000
40,000110,000

The model learns the relationship between advertising expenditure and sales.

Suppose the trained equation is:\[ Sales = 30,000 + 2(Advertising) \]

If advertising expenditure is ₹50,000:\[ Sales = 30,000 + 2(50,000) \]\[ Sales = ₹130,000 \]

Therefore, the predicted sales are ₹1,30,000.


9. Variables in Linear Regression

There are two important types of variables.

Independent Variable

The variable used to make the prediction.

It is represented by X.

Example:

Hours studied

Dependent Variable

The value that we want to predict.

It is represented by Y.

Example:

Marks

Therefore:

Independent Variable        Dependent Variable
       X                          Y
Hours Studied  ─────────→       Marks

10. Applications of Linear Regression

Linear regression is used for:

Business

  • Sales forecasting
  • Revenue prediction
  • Demand forecasting

Education

  • Predicting student marks
  • Studying relationship between study time and performance

Real Estate

  • House price prediction

Finance

  • Financial forecasting
  • Risk analysis

Marketing

  • Predicting sales from advertising expenditure
  • Predicting customer spending
  • Forecasting leads or conversions

11. Advantages of Linear Regression

  1. Simple and easy to understand.
  2. Easy to implement.
  3. Fast to train.
  4. Useful for numerical prediction.
  5. Easy to interpret the relationship between variables.
  6. Works well when the relationship between variables is approximately linear.

12. Limitations of Linear Regression

  1. It assumes a linear relationship between variables.
  2. It can be affected by outliers.
  3. It may not work well for complex relationships.
  4. Highly correlated independent variables can cause problems in multiple regression.
  5. It is not suitable when the output is a category such as Spam/Not Spam.

13. Linear Regression vs Logistic Regression

Linear RegressionLogistic Regression
Used for regressionMainly used for classification
Predicts continuous valuesPredicts class/probability
Output can be any numerical valueOutput probability is generally between 0 and 1
Example: House priceExample: Spam/Not Spam
Uses a linear equationUses a logistic/sigmoid function

⭐ Exam Answer

Linear Regression is a supervised machine learning algorithm used to predict a continuous numerical dependent variable based on one or more independent variables. It establishes a linear relationship between the input and output variables and finds the best-fit line using the least-squares method. The basic equation is \(y=mx+c\), or \(\hat{y}=b_0+b_1x\). Linear regression is widely used for house-price prediction, sales forecasting, salary prediction and demand forecasting.

⭐ Remember for the exam

Linear Regression = Supervised Learning + Numerical Prediction + Best-Fit Straight Line