Search code examples
pythonnumpymathsqrt

Compute Root Mean Squared Error and obtain a 3D array in Python


I have two 3d arrays A A.shape=[335,71,57] and B B.shape=[335,71,57] and I compute the RMSE between them in this way

rmse=sqrt(mean_squared_error(A,B))

and of course, I obtain a scalar. How could write it in order to obtain rmse.shape=[335,71,57] so another 3-dimensional array? In practice, I would need to obtain a rmse value for each position in the array.

thanks


Solution

  • Heres an example:

    A = np.random.rand(10,10,10)
    B = np.random.rand(10,10,10)
    mse = ((A-B)**2)
    rmse = np.sqrt(mse)
    

    The third line will get you per element squared error, the last line will get per element root.

    Note that what you are looking for is not the MSE, as the MSE is the mean of the squared error, and you are looking for per item.

    By adding mse = mse.mean(axis=ax) you can get the mean, in an axis you choose (before taking the root).

    For example:

    A = np.random.rand(10,10,10)
    B = np.random.rand(10,10,10)
    mse = ((A-B)**2).mean(axis=0)
    rmse = np.sqrt(mse)
    

    Will take per row RMSE.