1def percentage_error(actual, predicted):
2 res = np.empty(actual.shape)
3 for j in range(actual.shape[0]):
4 if actual[j] != 0:
5 res[j] = (actual[j] - predicted[j]) / actual[j]
6 else:
7 res[j] = predicted[j] / np.mean(actual)
8 return res
9
10def mean_absolute_percentage_error(y_true, y_pred):
11 return np.mean(np.abs(percentage_error(np.asarray(y_true), np.asarray(y_pred)))) * 100