python中的MAPE计算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47648133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:17:48  来源:igfitidea点击:

MAPE calculation in python

pythonpython-3.xpandasnumpyspyder

提问by Magg_rs

I want to calculate Mean Absolute percentage error (MAPE) of predicted and true values. I found a solution from here, but this gives error and shows invalid syntax in the line mask = a <> 0

我想计算预测值和真实值的平均绝对百分比误差 (MAPE)。我从这里找到了一个解决方案,但这给出了错误并在行中显示了无效的语法mask = a <> 0

    def mape_vectorized_v2(a, b): 
    mask = a <> 0
    return (np.fabs(a - b)/a)[mask].mean() 

   def mape_vectorized_v2(a, b): 
       File "<ipython-input-5-afa5c1162e83>", line 1
         def mape_vectorized_v2(a, b):
                                       ^
     SyntaxError: unexpected EOF while parsing

I am using spyder3. My predicted value is a type np.array and true value is dataframe

我正在使用 spyder3。我的预测值是 np.array 类型,真实值是数据框

type(predicted)
Out[7]: numpy.ndarray
type(y_test)
Out[8]: pandas.core.frame.DataFrame

How do i clear this error and proceed with MAPE Calculation ?

如何清除此错误并继续进行 MAPE 计算?

Edit :

编辑 :

predicted.head()
Out[22]: 
   Total_kWh
0   7.163627
1   6.584960
2   6.638057
3   7.785487
4   6.994427

y_test.head()
Out[23]: 
     Total_kWh
79         7.2
148        6.7
143        6.7
189        7.2
17         6.4

np.abs(y_test[['Total_kWh']] - predicted[['Total_kWh']]).head()
Out[24]: 
   Total_kWh
0        NaN
1        NaN
2        NaN
3        NaN
4   0.094427

回答by jezrael

In python for compare by not equal need !=, not <>.

在 python 中比较需要不相等!=,而不是<>.

So need:

所以需要:

def mape_vectorized_v2(a, b): 
    mask = a != 0
    return (np.fabs(a - b)/a)[mask].mean()

Another solution from stats.stackexchange:

来自stats.stackexchange 的另一个解决方案:

def mean_absolute_percentage_error(y_true, y_pred): 
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

回答by Pablo Andrés Casta?eda

Both solutions are not working with zero values. This is working form me:

两种解决方案都不适用于零值。这是我的工作:

def percentage_error(actual, predicted):
    res = np.empty(actual.shape)
    for j in range(actual.shape[0]):
        if actual[j] != 0:
            res[j] = (actual[j] - predicted[j]) / actual[j]
        else:
            res[j] = predicted[j] / np.mean(actual)
    return res

def mean_absolute_percentage_error(y_true, y_pred): 
    return np.mean(np.abs(percentage_error(np.asarray(y_true), np.asarray(y_pred)))) * 100

I hope it helps.

我希望它有帮助。

回答by Satyam Anand

Since the actual values can also be zeroes I am taking the average of the actual values in the denominator, instead of the actual values:

由于实际值也可以为零,我取分母中实际值的平均值,而不是实际值:

Error = np.sum(np.abs(np.subtract(data_4['y'],data_4['pred'])))
Average = np.sum(data_4['y'])
MAPE = Error/Average