How to find symmetric mean absolute error in python?
Try the following.
def mape(row):
return abs(row.Forecast - row.Actual) / ((abs(row.Actual) + abs(row.Forecast))/2)
# create the pandas dataframe if you dont have one already
df=pd.DataFrame.from_dict({'Actual':[2,3,4,5,6,7,8,9], 'Forecast':[1,3,5,4,6,7,10,7]})
# apply the above function on each row
smape = df.apply(mape, axis=1).sum() * (1/len(df))
Output: 0.19791666666666669
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))