What is Linear Regression? #
Linear Regression is one of the most widely used techniques in supervised machine learning. It is designed to understand and model the relationship between a target variable (output) and one or more input variables (features). The goal is to predict continuous numerical values by fitting a straight line that best represents the data pattern.
In simple terms, it finds a mathematical relationship that explains how changes in input variables affect the output.





Key Concepts #
- It assumes a linear relationship between input (independent variables) and output (dependent variable).
- The model determines a best-fit line that minimizes prediction errors.
- This line is calculated using the least squares method, which reduces the difference between actual and predicted values.
- It can be used for both simple (one variable) and multiple (multiple variables) predictions.
- The equation of linear regression is typically written as:
Y = mX + c
where:- Y = predicted value
- X = input feature
- m = slope (impact of X on Y)
- c = intercept (starting value)
Why Use Linear Regression? #
Linear Regression is popular because it is:
- Easy to understand and implement
- Fast to train compared to complex models
- Interpretable (you can clearly see how inputs affect output)
- Useful as a baseline model in machine learning projects
It is commonly applied in:
- Forecasting trends
- Business analytics
- Price prediction
- Risk analysis
Example: Predicting Exam Scores #
Imagine you want to estimate a student’s exam score based on how many hours they studied.
From observation, you notice a pattern:
As study time increases, exam scores also tend to improve.
In this case:
- Independent Variable (Input): Hours studied
→ This is the factor you measure or control - Dependent Variable (Output): Exam score
→ This is the outcome you want to predict
The regression model uses study hours to estimate the expected score by drawing a line that best fits the observed data.
How It Works (Step-by-Step) #
- Collect data (e.g., hours studied and exam scores)
- Plot the data on a graph
- Fit a straight line that best represents the data points
- Use the line to make predictions for new inputs
Important Assumptions #
For accurate results, Linear Regression relies on a few assumptions:
- The relationship between variables is linear
- Data points are independent
- Errors are normally distributed
- There is minimal multicollinearity (in multiple regression)
Best-Fit Line in Linear Regression #
Linear regression is a fundamental technique used to understand and model the relationship between variables. The best-fit line is a straight line that most closely represents how the input variable(s) (X) relate to the output variable (Y).
Instead of passing exactly through every point, this line is positioned in such a way that it reduces the overall prediction error between actual data values and predicted values.
Objective of the Best-Fit Line #
The main purpose of linear regression is to determine a line that minimizes the difference between observed values and predicted outcomes. This difference is commonly referred to as error or residual.
By minimizing this error, the model becomes more accurate and can be used to make reliable predictions on new data.

Understanding Variables #
- Y (Dependent Variable / Target)
This is the output we want to predict. - X (Independent Variable / Predictor)
This is the input used to predict the output.
Mathematical Representation #
A simple linear regression model can be written as:
Where:
- θ₁ (Intercept) → The value of Y when X = 0
- θ₂ (Slope) → The rate at which Y changes with respect to X
Key Idea #
The best-fit line is chosen such that it minimizes the total squared error between actual and predicted values. This method is known as:
Least Squares Method
Understanding the Best-Fit Line in Linear Regression #
Linear Regression is one of the most widely used techniques in supervised learning. It helps us understand how a dependent variable changes with respect to one or more independent variables. The main objective is to draw a straight line that best represents the relationship between the variables.
1. Purpose of the Best-Fit Line #
The goal of linear regression is to find a line that closely matches the given data points. This line is called the best-fit line because it minimizes the difference between the actual observed values and the values predicted by the model.
Instead of passing through every point, the line is positioned in such a way that the overall error between predictions and real values is as small as possible.
2. Mathematical Equation of the Line #
In simple linear regression (with one input variable), the equation of the best-fit line is:
Where:
- y → Predicted output (dependent variable)
- x → Input feature (independent variable)
- m → Slope of the line (rate of change)
- b → Intercept (value of y when x = 0)
What do these mean? #
- Slope (m): Shows how much the output changes when the input increases by one unit.
For example, if m=5, then y increases by 5 units for every 1 unit increase in x. - Intercept (b): Represents the starting point of the line on the y-axis.
3. Error Minimization using Least Squares #
To determine the best values of m and b, we use the Least Squares Method.
Residual (Error) #
The difference between the actual value and predicted value is called a residual:
Where:
- yi → Actual value
- y^i → Predicted value
Objective Function #
The goal is to minimize the Sum of Squared Errors (SSE):
Squaring ensures:
- All errors become positive
- Larger errors are penalized more heavily
This process guarantees that the final line fits the data as closely as possible.
4. Understanding the Best-Fit Line #
Once the model is trained:
- The slope explains the strength and direction of the relationship
- The intercept gives the baseline prediction
- The line can be used to make predictions for new input values
5. Key Assumptions of Linear Regression #
For reliable results, linear regression relies on some important assumptions:
- Linearity: The relationship between variables should be linear
- Independence: Observations should not depend on each other
- Homoscedasticity: Error variance should remain constant
- Normality of Errors: Residuals should be normally distributed
Hypothesis Function in Linear Regression #
In linear regression, the hypothesis function is the mathematical expression used to predict the value of the target variable based on input features. It defines how the input variables are mapped to the output.
In simple terms, it is the formula your model uses to make predictions.
1. Hypothesis Function (Single Variable) #
For a basic linear regression model with one independent variable, the hypothesis function is written as:
Explanation of Terms: #
- h(x) or ŷ → Predicted value of the target variable
- x → Input feature (independent variable)
- β₀ (Intercept) → Value of the prediction when x = 0
- β₁ (Slope/Coefficient) → Represents how much the prediction changes when x increases by one unit
Example:
If β₁ = 3, it means that for every 1-unit increase in x, the predicted value increases by 3 units.
2. Hypothesis Function (Multiple Variables) #
When there are multiple input features, the model becomes multiple linear regression, and the hypothesis function expands as:
Explanation: #
- x₁, x₂, …, xₖ → Different input features
- β₀ → Intercept term
- β₁, β₂, …, βₖ → Coefficients showing the impact of each feature
Each coefficient tells how strongly its corresponding variable affects the final prediction.
3. How the Hypothesis Function Works #
The hypothesis function:
- Takes input values (features)
- Multiplies them with learned coefficients
- Adds the intercept
- Produces a predicted output
This output is then compared with actual values to measure error and improve the model.
4. Role in Model Training #
The hypothesis function is central to training a linear regression model:
- Initially, coefficients (β values) are random
- The model adjusts them using optimization techniques (like Gradient Descent)
- The goal is to minimize prediction error
Over time, the hypothesis function becomes more accurate.
5. Vector Form (Advanced Representation) #
For better performance and scalability, especially in programming, the hypothesis function is often written in vector form:
Where:
- θ (theta) → Vector of parameters (β values)
- x → Feature vector
This form is widely used in libraries like NumPy and machine learning frameworks.
6. Key Characteristics #
- It assumes a linear relationship between inputs and output
- It is simple, fast, and easy to interpret
- Works well when features are properly scaled
Assumptions of Linear Regression #
Linear regression works best when certain conditions are met. These assumptions ensure that your model produces reliable and meaningful results.
1. Linearity #
The core idea of linear regression is that the relationship between the independent variables (X) and the dependent variable (Y) follows a straight-line pattern.
If the relationship is curved or complex, a simple linear model may not capture it properly.

2. Independence of Errors #
The prediction errors (residuals) should be independent of each other.
This means that one error should not influence another—especially important in time-series or sequential data.
3. Homoscedasticity (Constant Variance) #
The spread of errors should remain consistent across all levels of the input variables.
If the variance of errors increases or decreases (forming patterns like a funnel shape), it is called heteroscedasticity, which can weaken the reliability of the model.

4. Normal Distribution of Errors #
The residuals should follow a normal (bell-shaped) distribution.
This assumption is important for making valid statistical inferences like confidence intervals and hypothesis tests.
5. No Multicollinearity (Multiple Regression) #
In models with multiple input variables, predictors should not be highly correlated with each other.
Strong relationships between inputs can confuse the model and make it difficult to determine the true impact of each variable.
6. No Autocorrelation #
Residuals should not display patterns over time.
If errors are correlated (e.g., increasing or decreasing trends), it suggests the model is missing important information.
7. Additivity #
The effect of each independent variable on the dependent variable is assumed to be separate and additive.
This means the total impact is simply the sum of individual effects, without interaction unless explicitly modeled.
Types of Linear Regression #
Linear Regression can be categorized based on the number of input variables (features) used to predict the output. If the model uses only one input feature, it is called Simple Linear Regression. When multiple input features are involved, it is known as Multiple Linear Regression.
1. Simple Linear Regression #
Simple Linear Regression is applied when there is only one independent variable used to predict a dependent variable. It assumes that the relationship between input and output can be represented by a straight line.
Mathematical Representation #
Explanation of Terms #
- ŷ (y-hat): Predicted value of the dependent variable
- x: Independent variable (input feature)
- θ₀ (theta zero): Intercept (value of ŷ when x = 0)
- θ₁ (theta one): Slope (change in ŷ for a one-unit increase in x)
Example #
Suppose you want to estimate a person’s salary based on their years of experience. Here, experience is the input (x), and salary is the output (y).
Key Points #
- Works best when the relationship is approximately linear
- Easy to implement and interpret
- Sensitive to outliers (extreme values can affect the line significantly)
2. Multiple Linear Regression #
Multiple Linear Regression is used when two or more independent variables are used to predict a single dependent variable. It helps model more complex relationships compared to simple linear regression.
Mathematical Representation #
Explanation of Terms #
- x₁, x₂, …, xₙ: Input features
- θ₁, θ₂, …, θₙ: Coefficients (impact of each feature on output)
- θ₀: Intercept
- ŷ: Predicted output
Example #
Predicting house prices using multiple factors such as:
- Area (square feet)
- Number of bedrooms
- Location
- Age of the property
Key Points #
- Captures more realistic scenarios with multiple influencing factors
- Requires more data for accurate predictions
- Can suffer from multicollinearity (when input features are highly correlated)
