How can I calculate MAPE (Mean Absolute Percentage Error) using Python?

MAPE, or Mean Absolute Percentage Error, is a commonly used metric for measuring the accuracy of a forecast or prediction. It represents the average percentage difference between the actual values and the predicted values. In order to calculate MAPE using Python, you can follow these steps:

1. First, gather the actual values and predicted values for the data set you want to analyze.
2. Import the necessary libraries for performing mathematical operations and calculating MAPE in Python.
3. Use the formula: MAPE = (1/n) * Σ(|(actual – predicted)|/actual) * 100, where n is the number of data points.
4. Plug in the values in the formula and use a loop to iterate through all the data points.
5. Calculate the absolute value of the difference between the actual and predicted values, and divide it by the actual value.
6. Multiply the result by 100 and add it to a running sum.
7. After the loop, divide the sum by the number of data points to get the average.
8. The final result will be the MAPE for the given data set.
9. You can also use built-in functions or packages in Python, such as numpy or pandas, to make the calculation more efficient and accurate.
By following these steps, you can easily calculate MAPE using Python, which can help you evaluate and improve the accuracy of your predictions.

Calculate MAPE in Python


The mean absolute percentage error (MAPE) is commonly used to measure the predictive accuracy of models. It is calculated as:

MAPE = (1/n) * Σ(|actual – prediction| / |actual|) * 100

where:

  • Σ – a symbol that means “sum”
  • n – sample size
  • actual – the actual data value
  • prediction – the predicted data value

MAPE is commonly used because it’s easy to interpret and easy to explain. For example, a MAPE value of 11.5% means that the average difference between the predicted value and the actual value is 11.5%.

The lower the value for MAPE, the better a model is able to predict values. For example, a model with a MAPE of 5% is more accurate than a model with a MAPE of 10%. 

How to Calculate MAPE in Python

There is no built-in Python function to calculate MAPE, but we can create a simple function to do so:

import numpy as np

def mape(actual, pred): 
    actual, pred = np.array(actual), np.array(pred)
    return np.mean(np.abs((actual - pred) / actual)) * 100

We can then use this function to calculate the MAPE for two arrays: one that contains the actual data values and one that contains the predicted data values.

actual = [12, 13, 14, 15, 15,22, 27]
pred = [11, 13, 14, 14, 15, 16, 18]

mape(actual, pred)

10.8009

From the results we can see that the mean absolute percentage error for this model is 10.8009%. In other words, the average difference between the predicted value and the actual value is 10.8009%.

Cautions on Using MAPE

Although MAPE is easy to calculate and interpret, there are two potential drawbacks to using it:

1. Since the formula to calculate absolute percent error is |actual-prediction| / |actual| this means that MAPE will be undefined if any of the actual values are zero.

2. MAPE should not be used with low volume data. For example, if the actual demand for some item is 2 and the forecast is 1, the value for the absolute percent error will be |2-1| / |2| = 50%, which makes it seem like the forecast error is quite high, despite the forecast only being off by one unit.

x