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:
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
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.
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.
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))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With