Artificial Intelligence

Linear Regression Algorithm (Advanced Learners) (CBSE Class 12 Artificial Intelligence)

Class 12 · Artificial Intelligence

Linear Regression Algorithm (Advanced Learners)

Machine Learning enables computers to learn patterns from historical data and make predictions without being explicitly programmed. One of the simplest and most widely used supervised Machine Learning algorithms is Linear Regression. It is used to predict continuous numerical values by identifying the relationship between two or more variables.

Linear Regression is commonly used in Artificial Intelligence for forecasting house prices, predicting sales, estimating student performance, analyzing weather conditions, and many other real-world applications. In this chapter, you will understand the basic concept of Linear Regression and learn how to implement it using Python.

CBSE Note

This topic is meant for Advanced Learners. Students should focus on understanding the workflow and implementation rather than the mathematical derivation of the algorithm.


Learning Objectives

  • Understand the concept of Machine Learning.
  • Understand supervised learning.
  • Learn the Linear Regression algorithm.
  • Understand dependent and independent variables.
  • Implement Linear Regression using Python.
  • Interpret prediction results.

What is Machine Learning?

Machine Learning (ML) is a branch of Artificial Intelligence in which computers learn patterns from existing data and use those patterns to make predictions or decisions without being explicitly programmed.

Definition

Machine Learning is a technique that enables computers to learn from data and improve their performance automatically through experience.


Types of Machine Learning

Type Description
Supervised Learning Uses labelled data for prediction.
Unsupervised Learning Finds hidden patterns without labels.
Reinforcement Learning Learns through rewards and penalties.

Linear Regression belongs to the Supervised Learning category.


What is Linear Regression?

Linear Regression is a supervised Machine Learning algorithm that predicts the value of a dependent variable based on one or more independent variables by establishing a linear relationship between them.

Definition

Linear Regression is a Machine Learning algorithm used to predict continuous numerical values by fitting the best possible straight line through the available data.


Example of Linear Regression

Suppose the number of hours studied by students increases, their examination marks also tend to increase. Linear Regression identifies this relationship and predicts marks for a given number of study hours.

Study Hours (X) Marks (Y)
2 45
4 60
6 75
8 90

Independent and Dependent Variables

Variable Description
Independent Variable (X) The input used for prediction.
Dependent Variable (Y) The output predicted by the model.

In the above example:

  • Study Hours → Independent Variable (X)
  • Marks → Dependent Variable (Y)

Workflow of Linear Regression



Collect Dataset

      │

      ▼

Import Dataset

      │

      ▼

Prepare Data

      │

      ▼

Split Training & Testing Data

      │

      ▼

Train Model

      │

      ▼

Predict Results

      │

      ▼

Evaluate Model


Python Libraries Required

Library Purpose
Pandas Reading and manipulating data.
NumPy Numerical operations.
Matplotlib Visualization.
Scikit-learn Machine Learning implementation.

Importing Required Libraries



import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression


Step 1: Load the Dataset



import pandas as pd

df = pd.read_csv("studentmarks.csv")

print(df.head())


Step 2: Select Input and Output Variables



X = df[['StudyHours']]

Y = df['Marks']


Step 3: Split the Dataset

The dataset is divided into two parts:

  • Training Data
  • Testing Data


X_train, X_test, Y_train, Y_test = train_test_split(

X, Y,

test_size=0.2,

random_state=0

)


Step 4: Train the Model



model = LinearRegression()

model.fit(X_train, Y_train)


Step 5: Predict the Results



prediction = model.predict(X_test)

print(prediction)


Step 6: Compare Actual and Predicted Values



result = pd.DataFrame({

'Actual':Y_test,

'Predicted':prediction

})

print(result)


Expected Output

Actual Marks Predicted Marks
82 81.5
90 89.2
76 77.1

Real-Life Applications

  • House price prediction.
  • Sales forecasting.
  • Weather forecasting.
  • Student performance prediction.
  • Crop yield estimation.
  • Medical diagnosis support.
  • Stock market trend analysis.

Advantages of Linear Regression

  • Simple to understand.
  • Easy to implement.
  • Fast training.
  • Suitable for continuous data prediction.
  • Good baseline Machine Learning algorithm.

Limitations of Linear Regression

  • Assumes a linear relationship between variables.
  • Sensitive to outliers.
  • Less effective for complex datasets.
  • Cannot model highly non-linear relationships.

Case Study

A coaching institute wants to estimate students' examination marks based on the number of hours they study every day. Historical data containing study hours and marks is collected. A Linear Regression model is trained using this dataset. Once trained, the model can predict the expected marks of new students based on their study hours.


Think Like an AI Engineer

A real estate company has historical data containing the size of houses and their selling prices. Which Machine Learning algorithm would you recommend to predict the selling price of a new house?

Click to View Answer

Since house price is a continuous numerical value, Linear Regression is an appropriate algorithm for predicting the selling price.


Competency-Based Question

A hospital wants to predict patients' medical expenses based on their age and BMI. Explain how Linear Regression can help solve this problem.


Common Beginner Mistakes

  • Using categorical data without preprocessing.
  • Not separating input and output variables correctly.
  • Skipping train-test split.
  • Training the model before cleaning the dataset.
  • Using missing values without handling them.

Quick Revision

  • Linear Regression is a supervised Machine Learning algorithm.
  • Used for predicting continuous numerical values.
  • X = Independent Variable.
  • Y = Dependent Variable.
  • Scikit-learn provides the LinearRegression() class.

Memory Trick

Learn → Train → Predict


Exam Tips

  • Remember that Linear Regression belongs to supervised learning.
  • Know the difference between independent and dependent variables.
  • Understand the implementation steps in Python.
  • Practice the complete program in Google Colab or any Python IDE.

Summary

  • Linear Regression is one of the simplest Machine Learning algorithms.
  • It predicts continuous numerical values using historical data.
  • The algorithm learns the relationship between input and output variables.
  • Python libraries such as Pandas and Scikit-learn simplify its implementation.
  • Linear Regression is widely used in Artificial Intelligence for prediction and forecasting tasks.