如何使用numpy在python中计算RMSPE [英] How to calculate RMSPE in python using numpy
问题描述
我正在使用 Rossmann数据集进行多变量预测.现在,我需要使用RMSPE指标来评估我的模型.我在此处看到了相关公式.但是我不确定如何使用numpy有效地实现这一点.任何帮助深表感谢.
I am doing a multivariate forecasting using the Rossmann dataset. I now need to use the RMSPE metric to evaluate my model. I saw the relevant formula here. But I am not sure how to efficiently implement this using numpy. Any help is much appreciated.
推荐答案
您可以利用numpy的矢量化功能来实现这样的错误度量.以下函数可用于计算RMSPE:
You can take advantage of numpy's vectorisation capability for an error metric like this. The following function can be used to compute RMSPE:
def rmse(y_true, y_pred):
'''
Compute Root Mean Square Percentage Error between two arrays.
'''
loss = np.sqrt(np.mean(np.square(((y_true - y_pred) / y_true)), axis=0))
return loss
(对于向量之间的错误,axis=0
明确指出该错误是按行计算的,并返回向量.这不是必需的,因为这是np.mean
的默认行为.)
(For the error between vectors, axis=0
makes it explicit that the error is computed row-wise, returning a vector. It isn't required, as this is the default behaviour for np.mean
.)
这篇关于如何使用numpy在python中计算RMSPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!