Mean Squared Error in Numpy?

TheMeaningfulEngineer picture TheMeaningfulEngineer · May 27, 2013 · Viewed 172k times · Source

Is there a method in numpy for calculating the Mean Squared Error between two matrices?

I've tried searching but found none. Is it under a different name?

If there isn't, how do you overcome this? Do you write it yourself or use a different lib?

Answer

Saullo G. P. Castro picture Saullo G. P. Castro · Aug 4, 2013

You can use:

mse = ((A - B)**2).mean(axis=ax)

Or

mse = (np.square(A - B)).mean(axis=ax)
  • with ax=0 the average is performed along the row, for each column, returning an array
  • with ax=1 the average is performed along the column, for each row, returning an array
  • with ax=None the average is performed element-wise along the array, returning a scalar value