Hello! Let's begin our journey into supervised learning.
Introduction
In the previous modules, we explored unsupervised learning, where our goal was to find hidden patterns in data without any pre-defined labels, like with K-Means and GMMs. We're now shifting gears to supervised learning. Here, the dataset includes a specific target variable we want to predict. Our goal is to build a model that learns the mapping from input features to this target.
This week, we start with one of the most fundamental supervised learning algorithms: Linear Regression. It's used for regression tasks, where the goal is to predict a continuous numerical value—like the price of a house, the temperature tomorrow, or the weight of a person.
Today's learning outcome is to formulate linear regression as a model that minimizes the sum of squared errors. We'll focus on the core principle that defines how a linear regression model finds the "best-fit" line for a set of data.
The Linear Regression Model
At its heart, linear regression assumes a linear relationship between the input features and the target variable. For a simple case with one input feature () and one target variable (), this relationship is represented by the familiar equation of a straight line.
The following short video clearly introduces this equation and the terminology used in machine learning.
Linear Regression in 3 Minutes
Watch this clip from the 3-Minute Data Science channel to understand the basic equation of a linear model and the role of its coefficients.
Please watch from the beginning to 01:21. Pay attention to how the coefficients, often denoted as $\beta_0$ and $\beta_1$, control the intercept and slope of the line.
As the video explained, our model's prediction, which we'll call ("y-hat"), is given by:
- : The input feature (e.g., square footage of a house).
- : The predicted value (e.g., predicted price of the house).
- : The coefficient or weight associated with the feature . It represents the slope of the line—how much changes for a one-unit change in .
- : The intercept or bias. It's the value of when is zero.
The goal of "training" a linear regression model is to find the optimal values for and that make our line fit the data as closely as possible. But how do we define "closely"?
Measuring Error: The Sum of Squared Errors (SSE)
Imagine plotting your data points and drawing a random line through them. Some points will be above the line, some below. The vertical distance between each data point and the line is our error, or more formally, the residual.
Where is the actual, true value for the i-th data point.
To get a single number that represents the total error for the entire dataset, we can't just sum up the residuals, because positive and negative errors would cancel each other out. The standard solution is to square each residual and then sum them all up. This gives us the Sum of Squared Errors (SSE).
The next video provides an excellent, intuitive explanation of this entire process.
Linear Regression, Clearly Explained!!!
The StatQuest channel is fantastic at building intuition. This video walks through the idea of finding the line that results in the smallest possible sum of squared errors.
Watch from 01:18 to 03:36. The video visually demonstrates measuring residuals, squaring them, and searching for the line that minimizes this sum. This is the core idea behind the term 'least squares'.
The process described in the video—finding the line that has the least sum of squares—is exactly why the most common method for linear regression is called Ordinary Least Squares (OLS).
The image below provides a static visual that summarizes this concept perfectly. The vertical lines are the residuals, and the colored squares represent the squared residuals. The goal of OLS is to adjust the line to make the total area of all those squares as small as possible.

Formalizing the Objective
We've built the intuition, so now let's put it into a formal objective. Linear regression is an optimization problem where we want to find the parameters () that minimize our cost function, which is the SSE.
Sum of Squared Errors (SSE): Complete Guide to Measuring Model ...
This guide on the Sum of Squared Errors provides a clear mathematical definition and, crucially, explains why we use squared errors.
Please read the following sections from the article 'Sum of Squared Errors (SSE): Complete Guide...': 'The SSE Formula' and 'Key Components' (under 'Mathematical Foundation'): This provides the formal equation. 'The Optimization Objective' (under 'SSE in Least Squares Regression'): This explicitly connects SSE to the goal of linear regression. 'Why Squared Errors?': This is a key section that explains the practical and mathematical reasons for this choice. Focus on the points about differentiability and penalizing large errors.
To summarize the key points from the reading:
-
The Objective Function: We are trying to solve the following:
This is just the mathematical way of saying "find the and that result in the minimum Sum of Squared Errors."
-
Why Square the Errors?
- Penalizes Large Errors: A data point that is far from the line contributes much more to the total error than a point that is close, encouraging the model to pay more attention to large mistakes.
- Mathematical Convenience: The squared error function is smooth and has a single global minimum. This allows for efficient optimization, for example using calculus (taking derivatives to find the minimum) or iterative methods like gradient descent, which you may be familiar with from your work with neural networks.
Conclusion
In this lesson, we made the transition to supervised learning and established the foundational principle of linear regression.
Key Takeaways:
- Linear regression models a linear relationship between features () and a continuous target () using the equation .
- The "best" line is the one that minimizes the total error between the model's predictions () and the actual data ().
- We quantify this total error using the Sum of Squared Errors (SSE), which is the sum of all the squared vertical distances (residuals) from each point to the line.
- The process of finding the model coefficients () that minimize the SSE is called Ordinary Least Squares (OLS).
Preview of the Next Lesson
Now that you understand the objective of linear regression, the next step is to put it into practice. In our next lesson, you will fit a linear regression model using scikit-learn and interpret its coefficients. We will take the concepts from today and apply them to a real dataset.