How to find symmetric mean absolute error in python?
Question:
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
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))
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
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))
In case you’d prefer SMAPE = 0 when the actual and predicted values are both 0:
import numpy as np
def smape(A, F):
with np.errstate(divide='ignore', invalid='ignore'):
tmp = 2 * np.abs(F-A) / (np.abs(A) + np.abs(F))
tmp[np.isnan(tmp)] = 0
return np.sum(tmp) / len(tmp) * 100
A = np.array([2,3,4,5,6,7,0])
F = np.array([1,3,5,4,6,7,0])
print(smape(A, F))
>> 15.873015873015875
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
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))
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
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))
In case you’d prefer SMAPE = 0 when the actual and predicted values are both 0:
import numpy as np
def smape(A, F):
with np.errstate(divide='ignore', invalid='ignore'):
tmp = 2 * np.abs(F-A) / (np.abs(A) + np.abs(F))
tmp[np.isnan(tmp)] = 0
return np.sum(tmp) / len(tmp) * 100
A = np.array([2,3,4,5,6,7,0])
F = np.array([1,3,5,4,6,7,0])
print(smape(A, F))
>> 15.873015873015875