How to Calculate Mean Absolute Error

How to Calculate Mean Absolute Error

3 mins read103 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Nov 16, 2023 15:45 IST

Mean Absolute Error, or MAE is an evaluation metric used to calculate the performance of regression models in machine learning. This article will briefly discuss how to calculate MAE using examples and how it differs from other evaluation metrics.

2023_10_What-is-3.jpg

We use different evaluation metrics to calculate the regression models’ performance in machine learning. One such metric is Mean Absolute Error (MAE). In this article, we will discuss what exactly  MAE is and why it is so important.

So, without further delay, let’s discuss Mean Absolute Error.

Table of Content

Recommended online courses

Best-suited Machine Learning courses for you

Learn Machine Learning with these high-rated online courses

2.5 L
2 years
2.5 L
2 years
1.53 L
11 months
34.65 K
11 months
5.6 L
18 months
– / –
8 hours
– / –
6 months

What is Mean Absolute Error?

Mean Absolute Error, or MAE, is an evaluation metric used to evaluate the performance of the regression model. It measures the average of the errors’ magnitude between the predicted and actual values.

The mathematical formula for Mean Absolute Error is:

2023_10_mathematical-formula-of-mean-absolute-error-2.jpg

Let’s take an example:

Problem Statement: Imagine you are a data scientist working for a startup that sells handmade crafts online. The company recently implemented a new pricing algorithm to predict the price of crafts based on various features like size, material, and complexity. You want to evaluate the performance of this new algorithm.

Here’s a set of actual prices of crafts and the prices predicted by the algorithm:

Craft Item Actual Price ($) Predicted Price ($)
Necklace 25 28
Bracelet 15 14
Earrings 20 22
Ring 30 29
Brooch 40 38

Solution:

Here, n = 5,

MAE = 1/5 * (|25-28| + |15-14| + |20-22| + |30-29| + |40-38|) = 1/5 * (3+1+2+1+2) = 1/5 * (9) = 1.8

=> MAE = 3

From the above result, on average the pricing algorithm is off by $1.8.

How to Calculate Mean Absolute Error using Python?

In this section, we will discuss two differnet methods that are commonly used to calculate the Mean Absolute Error of a mchine learning model.

Method-1: Using Scikit Learn

Scikit-learn is one of the most common and popular libraries in machine learning. It provides a built-in function to calculate the MAE.

Let’s have an example, to get a better understanding.


 
from sklearn.metrics import mean_absolute_error
actual = [2, 3, 5, 5, 9]
predicted = [3, 3, 8, 7, 6]
mae = mean_absolute_error(actual, predicted)
print(mae)
Copy code

Output

1.8

Method-2: Using TensorFlow
If you are working with some deep learning models, then we use TensorFlow and its high-level library keras. TensorFlow provides a function to compute MAE.

Let’s have an example, to get a better understanding.


 
import tensorflow as tf
actual = tf.constant([2, 3, 5, 5, 9], dtype=tf.float32)
predicted = tf.constant([3, 3, 8, 7, 6], dtype=tf.float32)
mae = tf.keras.losses.MeanAbsoluteError()(actual, predicted)
print(mae.numpy())
Copy code

Output

1.8

Why to Choose Mean Absolute Error?

  • MAE provides a clear and direct interpretation. For example, the MAE of x means, on average, that the model’s predictions are off by ‘x’ units. This is extremely useful while explaining to non-technical stakeholders.
  • MAE is less sensitive to the outliers than the mean squared error metric. 
  • It treats all errors equally, i.e., it does not give any preference to underprediction or overprediction.
  • MAE operates on the same scale as the data it measures. This ensures that the error metric is in the same unit as the target variable.

Comparison of Mean Absolute Error with other Evaluation Metrics

Metric Description Sensitivity to Outliers Interpretability Emphasis on Larger Errors Application
MAE Average of absolute differences between actual and predicted values. Less sensitive High No When outliers are present and equal weight to all errors is desired.
MSE Average of squared differences between actual and predicted values. Highly sensitive Moderate (due to squaring) Yes When larger errors are particularly undesirable.
RMSE Square root of MSE. Highly sensitive Moderate Yes Provides a measure of error in the same units as the target variable.
MAPE Average of the absolute percentage differences between actual and predicted values. Varies High (expressed as a percentage) No When relative error is more important than absolute error. Not suitable when actual values can be zero.
R-squared Proportion of variance in the dependent variable explained by the independent variables. Varies High (expressed as a proportion) N/A Measures the goodness of fit of the model.

 

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio