Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mean Squared Error in Numpy?

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?

like image 224
TheMeaningfulEngineer Avatar asked May 27 '13 13:05

TheMeaningfulEngineer


Video Answer


2 Answers

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
like image 146
2 revs Avatar answered Oct 02 '22 17:10

2 revs


This isn't part of numpy, but it will work with numpy.ndarray objects. A numpy.matrix can be converted to a numpy.ndarray and a numpy.ndarray can be converted to a numpy.matrix.

from sklearn.metrics import mean_squared_error mse = mean_squared_error(A, B) 

See Scikit Learn mean_squared_error for documentation on how to control axis.

like image 29
Charity Leschinski Avatar answered Oct 02 '22 17:10

Charity Leschinski