MAPE calculation in Python
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping
--
Chapters
00:00 Mape Calculation In Python
00:48 Accepted Answer Score 38
01:11 Answer 2 Score 8
01:32 Answer 3 Score 0
01:46 Answer 4 Score 9
02:11 Thank you
--
Full question
https://stackoverflow.com/questions/4764...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #pandas #numpy #spyder
#avk47
    Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping
--
Chapters
00:00 Mape Calculation In Python
00:48 Accepted Answer Score 38
01:11 Answer 2 Score 8
01:32 Answer 3 Score 0
01:46 Answer 4 Score 9
02:11 Thank you
--
Full question
https://stackoverflow.com/questions/4764...
--
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