Reputation: 2919
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.
Upvotes: 2
Views: 13538
Reputation: 1668
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
(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
.)
Upvotes: 6
Reputation: 1
It should be normalized by ground truths.
def rmspe(y_true, y_pred):
return np.sqrt(np.nanmean(np.square(((y_true - y_pred) / y_true))))*100
Upvotes: -2