Suleka_28
Suleka_28

Reputation: 2919

How to calculate RMSPE in python using 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.

Upvotes: 2

Views: 13538

Answers (2)

Chris
Chris

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

Larry
Larry

Reputation: 1

It should be normalized by ground truths.

RMSPE equation

def rmspe(y_true, y_pred):
    return np.sqrt(np.nanmean(np.square(((y_true - y_pred) / y_true))))*100

Upvotes: -2

Related Questions