2.7 C
London
Thursday, November 28, 2024
HomeSoftware TutorialsPythonHow to Calculate MAPE in Python

How to Calculate MAPE in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories