# How to 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.