Hello! Welcome back to our module on supervised regression.
In the last lesson, you successfully built your first predictive model—a simple linear regression model—and learned how to interpret its coefficients. You now know how to describe the relationship a model has learned between a feature and a target. But that raises a critical question: how good is that model? Is it making accurate predictions?
Today, we'll answer that question. Your goal is to evaluate a regression model using key metrics: Mean Squared Error (MSE), Root Mean Squared Error (RMSE), Mean Absolute Error (MAE), and R-squared. Mastering these metrics is fundamental to your journey in data science, as they allow you to quantify your model's performance and compare different models against each other.
1. The Foundation of Evaluation: Understanding Errors (Residuals)
Before we dive into specific metrics, we need to be crystal clear on what we're measuring. Every evaluation metric for regression is built upon the concept of error, also known as the residual.
The residual is simply the difference between the actual value and the value your model predicted for a given data point.
A small residual means your model's prediction was close to the truth; a large residual means it was far off. The goal of our evaluation metrics is to aggregate all these individual residuals across your dataset into a single, informative score.
To get a solid visual and conceptual grasp of residuals, the following article provides an excellent explanation.
Understanding Regression Metrics - Datarock
This article, 'Understanding Regression Metrics' from Datarock, has some great visuals for understanding the core concepts. We'll use it to define the terms for predicted values and residuals.
Please read the section that starts right after the first code block and scatter plot. It begins with 'Here we can view the metric scores for our model...'. Read down to the end of that section, just before the heading for 'Mean Absolute Error (MAE)'. Focus on the definitions of y-hat (Ŷ) and residuals, and how the two images illustrate these concepts.
2. Metrics Based on Error Magnitude: MAE, MSE, and RMSE
The first family of metrics we'll explore directly measures the average size of the errors your model makes. They answer the question: "On average, how far off are my model's predictions?"
The following video provides a clear, step-by-step explanation of these three related metrics.
Error / Loss Functions for Regression: Mean Squared Error (MSE), Mean Absolute Error (MAE), RMSE
This video from Greg Hogg breaks down MAE, MSE, and RMSE using a practical housing data example, which should feel familiar from our last lesson.
Please watch from the beginning (00:08) to 08:21. The video is divided into neat sections for each metric. Pay close attention to: Mean Absolute Error (MAE): How it's calculated and why its interpretation is so straightforward. Mean Squared Error (MSE): Why we square the errors and how this penalizes larger mistakes more heavily. Root Mean Squared Error (RMSE): How taking the square root of MSE makes the metric more interpretable again.
Let's summarize the key characteristics of each metric from the video:
-
Mean Absolute Error (MAE):
- Calculation: The average of the absolute values of the residuals.
- Interpretation: It represents the average error in your predictions. A MAE of 500, as in the video's example, means that on average, the model's prediction is off by 500 rooms.
- Key Feature: It's in the same units as the target variable, making it very easy to explain to non-technical stakeholders.
- Calculation: The average of the absolute values of the residuals.
-
Mean Squared Error (MSE):
- Calculation: The average of the squared residuals.
- Interpretation: The units are squared (e.g., "squared rooms"), which makes direct interpretation difficult. A lower MSE is better, but the number itself isn't intuitive.
- Key Feature: Squaring the errors means that large errors are weighted much more heavily than small ones. A single very wrong prediction will have a much bigger impact on MSE than on MAE. This is useful when large errors are particularly undesirable.
- Calculation: The average of the squared residuals.
-
Root Mean Squared Error (RMSE):
- Calculation: The square root of the MSE.
- Interpretation: Like MAE, it is in the same units as the target variable. An RMSE of 810 means the "typical" or standard deviation of the prediction error is 810 rooms.
- Key Feature: It offers a balance: it's interpretable like MAE but retains MSE's sensitivity to large errors. It is one of the most commonly used metrics in regression.
- Calculation: The square root of the MSE.
3. The "Goodness of Fit" Metric: R-squared (R²)
While MAE and RMSE tell you the magnitude of the error, they don't tell you if that error is large or small in the context of the overall data. An RMSE of $5,000 might be terrible for predicting the price of a car but excellent for predicting the price of a house.
R-squared (R²), or the Coefficient of Determination, provides this context. It tells you what proportion of the variance in the target variable is explained by your model.
This concept is best understood with a clear, intuitive explanation.
R-squared, Clearly Explained!!!
The StatQuest video on R-squared is famous for making this concept click. It explains R-squared by comparing your model's performance to a very simple baseline.
Please watch from 01:30 to 10:47. Pay close attention to the core idea: Calculating the variation around the mean (a simple horizontal line). Calculating the variation around your regression line. How R-squared uses these two values to determine the percentage of variation your model accounts for.
Here’s the essence of R-squared:
- Scale: It ranges from 0 to 1 (though it can be negative if your model is worse than just predicting the mean).
- Interpretation: An R² of 0.81 means your model explains 81% of the total variation in the target data. The remaining 19% is unexplained by your model's features.
- A higher R² is generally better. An R² of 1 means a perfect fit, while an R² of 0 means your model is no better than just guessing the average value every time.
4. Practical Application with Scikit-Learn
You now understand the theory behind these four crucial metrics. Let's see how to calculate them using Python and scikit-learn. It's surprisingly straightforward.
The following article provides a handy reference with the exact sklearn.metrics functions you'll need.
Evaluation Metrics for Regression — MSE, MAE, RMSE, R² Score
This Medium article, 'Evaluation Metrics for Regression', serves as a great cheat sheet. It provides the formulas, pros and cons, and ready-to-use Python code for each metric.
Quickly scan through the sections for 'Mean Squared Error (MSE)', 'Mean Absolute Error (MAE)', 'Root Mean Squared Error (RMSE)', and 'R² Score'. Your main focus should be on the 'Code Example' provided for each one. This will show you the exact functions to import from sklearn.metrics.
To bring this all together, here is a single code block showing how you would evaluate a model's predictions in practice. Assume y_test holds the true values and y_pred holds your model's predictions.
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import numpy as np
# Let's imagine these are our true values and predicted values for house prices
y_test = np.array([300000, 450000, 550000, 780000])
y_pred = np.array([320000, 430000, 580000, 750000])
# Calculate MAE
mae = mean_absolute_error(y_test, y_pred)
print(f"Mean Absolute Error (MAE): ${mae:,.2f}")
# Calculate MSE
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error (MSE): {mse:,.2f} (in dollars-squared, not intuitive)")
# Calculate RMSE
# You can take the sqrt of MSE or use a built-in argument
rmse = np.sqrt(mse)
# Or: rmse = mean_squared_error(y_test, y_pred, squared=False)
print(f"Root Mean Squared Error (RMSE): ${rmse:,.2f}")
# Calculate R-squared
r2 = r2_score(y_test, y_pred)
print(f"R-squared (R²): {r2:.2f}")
Interpreting this example output:
- MAE: On average, our model's price prediction is off by $25,000.
- RMSE: The typical prediction error is about $25,500. It's slightly higher than MAE because it's more sensitive to the larger errors (like the $30,000 error on the third house).
- R²: Our model explains 96% of the variance in the house prices. This indicates a very strong fit.
Conclusion
Great work! You can now not only build a regression model but also rigorously evaluate its performance. This is a massive step forward in your practical data science skills.
Key Takeaways:
- Residuals () are the building blocks of all regression metrics.
- MAE gives you the average error in the target's original units, treating all errors equally. It's easy to interpret.
- MSE penalizes large errors heavily but is in squared units, making it hard to interpret directly.
- RMSE is the best of both worlds: it's in the original units like MAE but is sensitive to large errors like MSE. It's often the default choice.
- R-squared measures the proportion of variance your model explains, providing a "goodness of fit" score between 0 and 1.
| Metric | Units | Sensitivity to Outliers | Interpretation Focus |
|---|---|---|---|
| MAE | Same as target | Low | Average absolute error |
| MSE | Squared units | High | Penalizing large errors |
| RMSE | Same as target | High | Typical error magnitude |
| R² | None (0 to 1) | Medium | Proportion of variance explained |
Preview of the next lesson:
So far, we've only used a single feature to make predictions (e.g., predicting price from area). But what if we have more information, like the number of bedrooms or the age of the house? In the next lesson, you will learn to fit a multiple linear regression model to leverage multiple features at once. Your new evaluation skills will be essential for determining if adding more features actually improves your model's performance.