Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find symmetric mean absolute error in python?

How can I calculate symmetric mean absolute error in python using numpy or pandas? Is there are metric present in scikit sklearn ?

Example data:

Actual value:   2,3,4,5,6,7,8,9
Forecast value: 1,3,5,4,6,7,10,7

Formula for SMAPE see screenshot below:

enter image description here

How can I do it in python using pandas or numpy and calculate SMAPE.

Note: More info about SMAPE: https://en.wikipedia.org/wiki/Symmetric_mean_absolute_percentage_error

like image 967
stone rock Avatar asked Jul 20 '18 09:07

stone rock


People also ask

What is symmetric error in Python?

The SMAPE is one of the alternatives to overcome the limitations with MAPE forecast error measurement. In contrast to the mean absolute percentage error, SMAPE has both a lower bound and an upper bound, therefore, it is known as symmetric.

How is Smape calculated?

The absolute difference between At and Ft is divided by half the sum of absolute values of the actual value At and the forecast value Ft. The value of this calculation is summed for every fitted point t and divided again by the number of fitted points n.


2 Answers

It's pretty straightforward to convert the equation to numpy

import numpy as np

def smape(A, F):
    return 100/len(A) * np.sum(2 * np.abs(F - A) / (np.abs(A) + np.abs(F)))

A = np.array([2,3,4,5,6,7,8,9])
F = np.array([1,3,5,4,6,7,10,7])
print(smape(A, F))
like image 69
blue_note Avatar answered Oct 27 '22 21:10

blue_note


I commented on the accepted answer but if you just want to copy and paste:

import numpy as np

def smape(A, F):
    tmp = 2 * np.abs(F - A) / (np.abs(A) + np.abs(F))
    len_ = np.count_nonzero(~np.isnan(tmp))
    if len_ == 0 and np.nansum(tmp) == 0: # Deals with a special case
        return 100
    return 100 / len_ * np.nansum(tmp)

A = np.array([2,3,4,5,6,7,8,0])
F = np.array([1,3,5,4,6,7,10,0])
print(smape(A, F))
like image 42
Bestname Avatar answered Oct 27 '22 21:10

Bestname