How to calculate MAPE with 0 values

Calculate MAPE when actual is 0

MAPE (mean absolute percentage error) is a metric used for the assessment of regression machine learning models. One issue that arises from MAPE, however, is that when your actual values can be or are close to 0, the MAPE calculation encounters the division by 0 problem and your MAPE value becomes infinitely large.

Why does MAPE become very large whilst MAE is small?

The reason that your MAPE value is very large whilst your Mean Absolute Error (MAE) is small is because you likely have actual values that are or are close to 0, meaning that in the following MAPE calculation:

calculate MAPE when actual is 0

One or more of the At values is 0, returning an infinite value. When calculating MAE there is no division, as it is the absolute difference between the actual and predicted values, so these 0 values will not effect this metric. This odd situation becomes clear if we look at an example:

Actual Predicted Absolute Error Absolute Percentage Error
10 12 2 0.2
5 2 3 0.5
8 7 1 0.125
0 1 1 inf (div by 0)

So from this example the MAE will be (2+3+1+1)/4 = 1.75, but the MAPE will be (0.2+0.5+0.125+inf)/4 = inf.

How can I use MAPE with zero values?

When your dataset has actual values around 0, using MAPE is not possible as it returns very large values. So, now that you can't use MAPE, what should you do? Well, here are your options if the absolute error is what you're interested in:

  1. Use MAE. This metric is not unstable at 0 values and is a very common metric for regression models
  2. Use sMAPE. This metric (symmetric mean absolute percentage error) seeks to address to 0 problem, and there are several different calculations available depending on your use case. Similar to MAPE, it also returns a percentage error.

Regression metrics

MDAPE
R2
MSE
RMSE
Negative percent error

Metric comparisons

RMSE vs MAPE, which is the best regression metric?

Metric calculators

MAPE calculator

References

MAPE sklearn documentation

Stephen Allwright

Stephen Allwright

I'm a Data Scientist currently working for Oda, an online grocery retailer, in Oslo, Norway. These posts are my way of sharing some of the tips and tricks I've picked up along the way.
Oslo, Norway