3
我需要一個MAPE函數,但是我無法在標準包中找到它...下面是我的這個函數的實現。如何優化Python中的MAPE代碼?
def mape(actual, predict):
tmp, n = 0.0, 0
for i in range(0, len(actual)):
if actual[i] <> 0:
tmp += math.fabs(actual[i]-predict[i])/actual[i]
n += 1
return (tmp/n)
我不喜歡它,它在速度方面超級不是最佳。如何將代碼重寫爲Pythonic方式並提高速度?