Linear Regression in Machine Learning
Introduction
Linear Regression is one of the most fundamental algorithms in Machine Learning. It is widely used for predicting continuous values such as prices, sales, and trends.
In this lesson, you will learn how Linear Regression works, its types, and how to implement it in Python.
What is Linear Regression?
Linear Regression is a supervised learning algorithm used to predict a continuous output based on one or more input variables.
It finds a relationship between input (independent variable) and output (dependent variable).
Simple Example
Predicting house prices based on area.
Linear Regression Equation

Where:
y = predicted value
x = input feature
m = slope of the line
b = intercept
The goal is to find the best line that fits the data.
Types of Linear Regression
Simple Linear Regression
Uses one independent variable
Example:
Predict salary based on experience
Multiple Linear Regression
Uses multiple independent variables
Example:
Predict house price based on area, location, and number of rooms
How Linear Regression Works
- Collect data
- Plot data points
- Fit a line through the data
- Minimize the error between predicted and actual values
The algorithm tries to find the best-fit line that reduces prediction errors.
Cost Function
The performance of Linear Regression is measured using a cost function.
J=1n∑(yactual−ypredicted)2J = \frac{1}{n} \sum (y_{\text{actual}} – y_{\text{predicted}})^2
This function calculates the error between actual and predicted values.
Assumptions of Linear Regression
- Linear relationship between variables
- No multicollinearity
- Homoscedasticity (constant variance)
- Normally distributed errors
Advantages of Linear Regression
- Simple and easy to understand
- Fast to train
- Works well with small datasets
- Interpretable results
Limitations of Linear Regression
- Cannot handle complex relationships
- Sensitive to outliers
- Assumes linear relationship
Implementation in Python
from sklearn.linear_model import LinearRegression
import numpy as np
X = np.array([[1], [2], [3], [4]])
y = np.array([10, 20, 30, 40])
model = LinearRegression()
model.fit(X, y)
prediction = model.predict([[5]])
print(prediction)
Real-World Applications
- House price prediction
- Sales forecasting
- Stock trend analysis
- Risk assessment
Conclusion
Linear Regression is the foundation of many Machine Learning algorithms. Understanding it helps you build a strong base for advanced models.
In the next lesson, you will learn about Logistic Regression, which is used for classification problems.
FAQs
What is Linear Regression used for?
It is used to predict continuous values such as prices and trends.
What is the difference between simple and multiple regression?
Simple uses one input variable, while multiple uses more than one.
Is Linear Regression easy to learn?
Yes, it is one of the easiest Machine Learning algorithms.
What is the cost function?
It measures the error between predicted and actual values.
Can Linear Regression handle complex data?
No, it works best with linear relationships.
Internal Link
To explore more courses and improve your skills, click here for more free courses



