且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用numpy在python中计算RMSPE

更新时间:2021-07-10 01:27:53

您可以利用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.)