The Python Oracle

MAPE calculation in Python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction

--

Chapters
00:00 Question
01:09 Accepted answer (Score 38)
01:34 Answer 2 (Score 8)
01:59 Answer 3 (Score 6)
02:36 Answer 4 (Score 0)
03:38 Thank you

--

Full question
https://stackoverflow.com/questions/4764...

Question links:
[here]: https://stackoverflow.com/questions/4225...

Accepted answer links:
[stats.stackexchange]: https://stats.stackexchange.com/a/294069

Answer 3 links:
[here]: https://scikit-learn.org/stable/modules/...

Answer 4 links:
[sklearn.metrics.mean_absolute_percentage_error]: https://scikit-learn.org/stable/modules/...
[Here]: https://github.com/scikit-learn/scikit-l...
[Source]: https://scikit-learn.org/stable/modules/...
[Source]: https://scikit-learn.org/stable/modules/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #pandas #numpy #spyder

#avk47



ACCEPTED ANSWER

Score 38


In Python for compare by not equal need !=, not <>.

So need:

def mape_vectorized_v2(a, b): 
    mask = a != 0
    return (np.fabs(a - b)/a)[mask].mean()

Another solution from stats.stackexchange:

def mean_absolute_percentage_error(y_true, y_pred): 
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100



ANSWER 2

Score 9


The new version of scikit-learn (v0.24) has a function that will calculate MAPE. sklearn.metrics.mean_absolute_percentage_error

All what you need is two array-like variables: y_true storing the actual/real values, and y_pred storing the predicted values.

You can refer to the official documentation here.




ANSWER 3

Score 8


Both solutions are not working with zero values. This is working form me:

def percentage_error(actual, predicted):
    res = np.empty(actual.shape)
    for j in range(actual.shape[0]):
        if actual[j] != 0:
            res[j] = (actual[j] - predicted[j]) / actual[j]
        else:
            res[j] = predicted[j] / np.mean(actual)
    return res

def mean_absolute_percentage_error(y_true, y_pred): 
    return np.mean(np.abs(percentage_error(np.asarray(y_true), np.asarray(y_pred)))) * 100

I hope it helps.




ANSWER 4

Score 0


Since the actual values can also be zeroes I am taking the average of the actual values in the denominator, instead of the actual values:

Error = np.sum(np.abs(np.subtract(data_4['y'],data_4['pred'])))
Average = np.sum(data_4['y'])
MAPE = Error/Average