Linear Regression (OLS)
Fit a line to a dataset by minimizing the Mean Squared Error (MSE). Drag the regression line manually to see how the residuals (red lines) change, or compute the exact best fit.
Fit a line to a dataset by minimizing the Mean Squared Error (MSE). Drag the regression line manually to see how the residuals (red lines) change, or compute the exact best fit.
Linear Regression is a fundamental algorithm in machine learning used to model the relationship between two variables. The goal is simple: find the straight line that best fits a set of data points. This line, often called the "regression line" or "hypothesis," is represented by the classic equation y = mx + c, where 'm' is the slope and 'c' is the y-intercept.
How do we know which line is the "best"? We measure its error. For each data point, the vertical distance between the point and our line is called the residual or error. These are the red lines in the visualization. A good line should have small residuals.
To measure the total error for the entire dataset, we use a loss function. A common and effective one is the Mean Squared Error (MSE). Here's how it works:
The "best" line is the one with the lowest possible MSE. Your goal is to find the values of 'm' and 'c' that minimize this loss.
While you can manually drag the line to try and find the best fit, there's a direct mathematical method to find the perfect 'm' and 'c'. This method is called Ordinary Least Squares (OLS). It's a formula that takes all the data points and instantly calculates the slope and intercept that guarantee the minimum possible Mean Squared Error. When you click the "Snap to Best Fit" button, you are seeing the OLS solution.
Use the interactive visualization to build your intuition.
Fit a line to a dataset by minimizing the Mean Squared Error (MSE). Drag the regression line manually to see how the residuals (red lines) change, or compute the exact best fit.