如何在python中找到对称平均绝对误差?

时间:2018-07-20 09:53:07

标签: python pandas numpy scikit-learn statistics

如何使用numpy或pandas在python中计算对称平均绝对误差? scikit sklearn中是否存在度量标准?

示例数据:

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

SMAPE的公式,请参见下面的屏幕截图:

enter image description here

我该如何在Python中使用pandas或numpy并计算SMAPE。

注意:有关SMAPE的更多信息:https://en.wikipedia.org/wiki/Symmetric_mean_absolute_percentage_error

3 个答案:

答案 0 :(得分:8)

将方程式转换为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))

答案 1 :(得分:1)

尝试以下操作。

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))

输出:0.19791666666666669

答案 2 :(得分:1)

我对接受的答案进行了评论,但是如果您只想复制并粘贴:

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))
    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))