TLDR
In this Mage Academy lesson on model evaluation, we’ll learn how to calculate Mean absolute error (MAE).
Glossary
Definition
Calculation
How to code
Definition
Mean absolute error (MAE) is a metric that is used to evaluate the performance of regression models. It’s defined as the average of the absolute difference between actual and predicted values.
Note
: When there are outliers in the data, MAE is a preferred metric to assess the model's performance.
Calculation
MAE is calculated using the following formula.
Mathematically we can write MAE as shown below:
When the MAE value is close to
0
, we claim the model is at its best. This means that most of the predicted values are identical to the actual ones.
Source: GIPHY
Imagine a scenario where you’ve successfully created a regression model to predict the weight of a person based on their height, and now you want to evaluate the model. For this you gathered heights and weights of 5 people and then predicted their weights.
Now, let’s use the regression metric MAE to evaluate the performance of the model.
How to code
From Scratch
To calculate MAE we need actual (true_w) and predicted (pred_w) values.
Step-1
: Calculate the
error
(err) by finding the difference between the actual (true_w) and predicted weight(pred_w) values.
Step-2
: Calculate the absolute values (abs_err) for all error (err) values
.
Let’s first understand what absolute values are:
The absolute value of a number is the non-negative value of the number.
For example,
The absolute value of -15 is 15
The absolute value of -3.45 is 3.45
The absolute value of 56 is 56
We can convert a number into an absolute value in 2 ways as shown:
By multiplying the negative number with
-1
.
We can use Python’s default
abs()
method to calculate the absolute values.
In this lesson, we’ll calculate the absolute values using the
abs()
method.
Step-3
: Calculate the sum of all absolute error (abs_err) values.
Step-4
: Calculate the average or mean of all absolute errors (abs_err).
Scikit-learn library
As you've seen, calculating MAE from scratch is time-consuming, so let's calculate MAE using Scikit-learn, Python's machine learning library.
To calculate MAE,
Import the
mean_absolute_error
function from the sklearn
metrics
module.
Pass the actual (true_w) and predicted (pred_w) values into the
mean_absolute_error
function.
Conclusion
From the MAE value, we can tell that the weight prediction model is a better model, but it's not the best as the MAE value is not close to 0. This is how we utilize the MAE metric to assess regression model performance.
Want to learn more about machine learning (ML)? Visit
! ✨🔮