How to Calculate MAPE in Python? (original) (raw)

Last Updated : 12 Sep, 2025

Mean Absolute Percentage Error (MAPE), also known as Mean Absolute Percentage Deviation (MAPD), is a popular metric to evaluate the accuracy of forecasts. It shows the average percentage difference between the actual and predicted values.

MAPE measures how accurate your forecast or model predictions are:

A lower MAPE means better prediction accuracy and a MAPE of **0 means perfect predictions.

Example Dataset

Below is a small dataset showing actual and forecasted daily sales values:

**Day No. **Actual Sales **Forecast Sales **Absolute Percentage Error (APE)
1 136 134 0.014
2 120 124 0.033
3 138 132 0.043
4 155 141 0.090
5 149 149 0.0

**APE formula:

APE = \frac{|Actual - Forecast|}{Actual}

**MAPE formula

Now, the MAPE value can be found by taking the mean of the APE values.

MAPE = \frac{1}{n}\sum_{i=1}^{n} \frac{|Actual_i - Forecast_i|}{Actual_i} \times 100

Method 1: Calculate MAPE using Lists

Python `

Define the dataset as Python lists

actual = [136, 120, 138, 155, 149] forecast = [134, 124, 132, 141, 149]

Store APE values

APE = []

Calculate APE for each record

for i in range(len(actual)): per_err = abs((actual[i] - forecast[i]) / actual[i]) APE.append(per_err)

Calculate MAPE

MAPE = sum(APE) / len(APE)

Display the result

print(f"MAPE : {round(MAPE, 2)}") print(f"MAPE % : {round(MAPE * 100, 2)}%")

`

Output

MAPE : 0.04 MAPE % : 3.64%

Here, the forecast has an average error of **3%, which indicates good accuracy.

Method 2: Calculate MAPE using NumPy and pandas

If you are already working with NumPy arrays or pandas DataFrames, you can calculate MAPE more directly without creating a custom function.

**Using NumPy

Python `

import numpy as np

actual = np.array([136, 120, 138, 155, 149]) predicted = np.array([134, 124, 132, 141, 149])

mape = np.mean(np.abs((actual - predicted) / actual)) * 100 print("MAPE (NumPy):", round(mape, 2), "%")

`

**Output:

MAPE (NumPy): 3.64 %

**Using pandas

Python `

import pandas as pd

df = pd.DataFrame({ "actual": [136, 120, 138, 155, 149], "predicted": [134, 124, 132, 141, 149] })

mape = ((df["actual"] - df["predicted"]).abs() / df["actual"]).mean() * 100 print("MAPE (pandas):", round(mape, 2), "%")

`

**Output:

MAPE (pandas): 3.64 %

This approach is short and efficient when working with numerical or tabular data and avoids manually looping through each record.