How to Calculate MAPE in Python

Spread the love

Introduction

As the world becomes increasingly data-driven, the importance of evaluating predictive models continues to surge. One widely used metric to assess the performance of forecasting models in various domains, such as finance, economics, and supply chain management, is the Mean Absolute Percentage Error (MAPE). This article provides an extensive overview of what MAPE is, why it’s essential, and how you can calculate it using Python.

Understanding MAPE

Before we dive into the Python code, it’s critical to grasp the concept of MAPE. MAPE quantifies the accuracy of a forecasting model by calculating the average percentage difference between the actual and predicted values. The formula is:

MAPE = (1/n) * Σ(|(Actual – Predicted)| / |Actual|) * 100

Where:

  • n is the number of data points
  • Σ represents the sum of the errors over all data points
  • |Actual – Predicted| is the absolute error
  • |Actual| is the absolute actual value

MAPE provides a clear indication of the accuracy level in percentage terms. However, it has limitations such as being undefined for actual values of zero and excessively penalizing predictions when actual values are low.

Loading Data

You will need a dataset with actual and predicted values to calculate MAPE. You can either use an existing dataset or generate synthetic data. For this article, let’s create synthetic data using pandas:

import pandas as pd

# Create a DataFrame with actual and predicted values
data = {'Actual': [100, 200, 300, 400, 500], 'Predicted': [110, 190, 320, 390, 490]}
df = pd.DataFrame(data)

Implementing MAPE Calculation

Now, let’s write a function that takes the actual and predicted values and returns the MAPE:

def calculate_mape(actual, predicted):
    """
    Calculate the Mean Absolute Percentage Error (MAPE)
    
    :param actual: list of actual values
    :param predicted: list of predicted values
    :return: MAPE as a percentage
    """
    # Ensure actual and predicted lists have the same length
    if len(actual) != len(predicted):
        raise ValueError("Input lists must have the same length")
    
    # Calculate MAPE
    n = len(actual)
    total_error = 0
    for a, p in zip(actual, predicted):
        # Ensure actual value is not zero
        if a == 0:
            raise ValueError("Actual values must be non-zero")
        
        total_error += abs((a - p) / a)
    
    mape = (total_error / n) * 100
    return mape

You can now use this function to calculate MAPE for your dataset.

actual = df['Actual'].tolist()
predicted = df['Predicted'].tolist()

mape = calculate_mape(actual, predicted)
print(f'MAPE: {mape}%')

Refining the Implementation with NumPy

To optimize the code, you can leverage NumPy’s vectorized operations for faster and more concise calculations.

import numpy as np

def calculate_mape_numpy(actual, predicted):
    """
    Calculate the Mean Absolute Percentage Error (MAPE) using numpy
    
    :param actual: numpy array of actual values
    :param predicted: numpy array of predicted values
    :return: MAPE as a percentage
    """
    # Ensure actual and predicted arrays have the same length
    if actual.shape != predicted.shape:
        raise ValueError("Input arrays must have the same shape")
    
    # Ensure there are no zero values in the actual array
    if np.any(actual == 0):
        raise ValueError("Actual values must be non-zero")
    
    # Calculate MAPE using numpy
    mape = np.mean(np.abs((actual - predicted) / actual)) * 100
    return mape

You can use this function in a similar way.

actual_np = np.array(actual)
predicted_np = np.array(predicted)

mape = calculate_mape_numpy(actual_np, predicted_np)
print(f'MAPE (using numpy): {mape}%')

Conclusion

In this comprehensive guide, we explored the Mean Absolute Percentage Error (MAPE), understood its significance, and implemented its calculation in Python. Furthermore, we improved our initial implementation using NumPy’s vectorized operations for a more efficient calculation. While MAPE is a valuable metric in forecasting, it is important to understand its limitations and consider alternative metrics when appropriate.

Leave a Reply