Hello! Welcome to the fourth lesson in the "Foundations of Machine Learning" module.
In our last lesson, we focused on essential data preprocessing techniques: handling missing values and scaling our features. You learned that scaling is particularly important for algorithms like linear regression. Now that we know how to prepare our data, it's time to build our first predictive model.
This lesson dives into the fundamental building block of regression models. Your learning outcome is to describe the mathematical formulation of a simple linear regression model. We will move from the simple idea of drawing a line through data points to a precise mathematical equation that forms the basis of many advanced machine learning concepts. Understanding this is crucial for your GATE preparation and for building a solid foundation in data science.
1. The Goal: Modeling a Relationship
At its core, linear regression is about finding a linear relationship between two variables:
- An independent variable (also called the input, feature, or predictor), which we'll denote as .
- A dependent variable (also called the output, target, or response), which we'll denote as .
The goal is to use the value of to predict the value of . For example, we might want to predict a house's price () based on its size ().
To understand the core objectives and terminology of linear regression, let's start with a short introductory video.
Video 1: Introduction to Simple Linear Regression
This video from dataminingincae provides a clear introduction to the main goals of simple linear regression and defines the roles of dependent and independent variables.
Please watch the video from the beginning until 03:40. Focus on understanding: The two main objectives: establishing a relationship and forecasting. The definitions of the dependent variable (Y) and independent variable (X).
As the video explained, we are looking for a straight line that best represents the data. You might remember the equation of a line from school as . In machine learning, we use slightly different notation, but the concept is exactly the same.
2. The Mathematical Formulation of the Model
The primary task in linear regression is to find the specific straight line that best "fits" our data. To do this, we need a formal equation.
The Hypothesis Function
In machine learning, the model we create is often called a hypothesis function, usually denoted as . This function takes an input and outputs a predicted value, which we call ("y-hat").
For simple linear regression, the hypothesis function is the equation of a line:
Let's break down the components:
- : The predicted value of the target variable.
- : The input feature.
- (theta-zero): The intercept of the line. This is the predicted value of when . It's the "starting point" of our line on the y-axis.
- (theta-one): The slope or coefficient of the line. This value determines how much changes for a one-unit increase in .
The terms and are the parameters of our model. The "learning" part of machine learning is the process of finding the optimal values for these parameters.
You will encounter different notations for this same equation in various resources. It's important to recognize them:
h(x) = θ₀ + θ₁x(Common in ML courses, like Andrew Ng's)y = β₀ + β₁x(Common in statistics)y = w₀ + w₁x(Herewstands for "weights," common in deep learning)y = b + mx(Connects to classic algebra, wherebis the bias/intercept)
All these equations represent the same simple linear model. We will primarily use the notation.
Accounting for Real-World Noise: The Error Term
In the real world, data points rarely fall perfectly on a straight line. There's always some "noise" or variability that our model can't explain. To account for this, the full statistical model of linear regression includes an error term, denoted by (epsilon).
The complete equation for an actual observed data point is:
Here:
- is the actual, observed value for the i-th data point.
- is the model's predicted value ().
- is the random error for that data point—the difference between the actual value and the predicted value on the line.
The image below provides an excellent visual breakdown of this equation.

To see a practical interpretation of these parameters, let's return to the video.
Video 1: Introduction to Simple Linear Regression
This part of the video explains how the intercept, slope, and error term come together in a real-world model. It uses graphical examples to show how changing the parameters affects the line.
Please watch from 03:28 to 08:21. Pay attention to: How the intercept (β_0) shifts the line up or down. How the slope (β_1) changes the steepness and direction of the line. Why the error term (ε) is necessary to model real-world data that isn't perfectly linear.
3. Measuring How "Good" the Fit Is: The Cost Function
We now have a model, . But how do we find the best values for and ? We need a way to measure how well a particular line (defined by a specific and ) fits our data.
This is the job of the cost function (also called a loss function or objective function). The cost function quantifies the total error between our model's predictions () and the actual target values () across the entire training dataset.
For linear regression, the most common cost function is the Mean Squared Error (MSE).
Let's formalize this. In a supervised learning problem, we have a training set of examples: .
For each example , our prediction is .
The MSE cost function, denoted as , is defined as:
Substituting our hypothesis function, we get:
Let's dissect this formula:
- : This is the error for a single training example , the vertical distance between the prediction and the actual value.
- : We square the error. This does two things: it makes all errors positive (so they don't cancel each other out) and it penalizes larger errors more heavily.
- : We sum up the squared errors for all training examples.
- : We take the average of the squared errors.
- : This is a mathematical convention. Multiplying by makes the derivative calculation (which we'll see in the next lesson) cleaner. It doesn't change the values of and that minimize the function.
The goal of our learning algorithm is to find the values of and that result in the smallest possible value for .
The following resource provides a clear walkthrough of framing the linear regression problem and introducing the cost function.
A Deep Dive into Linear Regression
This article from Towards AI introduces the standard notation used in machine learning and builds up the intuition for the cost function.
Please read sections 1 and 2, focusing on: Standard Notation: Understand the meaning of x, y, m, and (x^(i), y^(i)). Representing the Model: See how the linear model is represented. The Cost Function: Reinforce your understanding of why we need a cost function to measure model quality.
Test your understanding!
Suppose you have a tiny dataset of 2 training examples:
- House size sq ft, Price thousand
- House size sq ft, Price thousand
You want to test a model with parameters and .
Calculate the Mean Squared Error cost for this model on this dataset. (You can ignore the for this exercise, as it's just a scaling factor).
Show answer
First, let's calculate the predictions () for each data point:
Next, let's calculate the squared error for each data point:
- Error 1:
- Error 2:
Finally, calculate the Mean Squared Error (with ).
The cost for this specific line is 1250. Our goal in the next lesson will be to find parameters that make this cost even lower.
Conclusion
In this lesson, we have formally defined our first machine learning model. This is a significant step! You've moved from an intuitive idea to a concrete mathematical structure.
Key Takeaways:
- Simple linear regression models the relationship between an input and an output using a straight line.
- The model's prediction is given by the hypothesis function: .
- (intercept) and (slope) are the model parameters that the algorithm learns from the data.
- The full statistical model includes an error term to account for noise: .
- To find the best parameters, we need to minimize a cost function. The standard choice is the Mean Squared Error (MSE), which measures the average squared difference between predictions and actual values.
Preview of the Next Lesson:
We've defined our goal: find the and that minimize the cost function . But how do we do that? In the next lesson, you will learn to apply gradient descent to optimize the cost function of a linear regression model. This powerful optimization algorithm is the engine that drives the learning process for linear regression and many other models in machine learning.