# Mean absolute percentage error (MAPE) in Scikit-learn

On CrossValidated, the StackExchange for statistics, someone asks:

How can we calculate the Mean absolute percentage error (MAPE) of our predictions using Python and scikit-learn?

Mean Absolute Percentage Error (MAPE) is an metric used to determine the success of a regression analysis. Read my answer on CV here:

**Cross posting my answer here:**

As noted (for example, in Wikipedia), MAPE can be problematic. Most pointedly, it can cause division-by-zero errors. My guess is that this is why it is not included in the sklearn metrics.

However, it is simple to implement.

Use like any other metric…:

> y_true = [3, -0.5, 2, 7]; y_pred = [2.5, -0.3, 2, 8] > mean_absolute_percentage_error(y_true, y_pred) Out[19]: 17.738095238095237

(Note that I’m multiplying by 100 and returning a percentage.)

… but with caution:

> y_true = [3, 0.0, 2, 7]; y_pred = [2.5, -0.3, 2, 8] > #Note the zero in y_true > mean_absolute_percentage_error(y_true, y_pred) -c:8: RuntimeWarning: divide by zero encountered in divide Out[21]: inf

Advertisements