Mean Squared Error in Numpy?

前端 未结 6 1481
忘掉有多难
忘掉有多难 2020-12-13 12:01

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?

I

相关标签:
6条回答
  • 2020-12-13 12:15

    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
    0 讨论(0)
  • 2020-12-13 12:22

    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.

    0 讨论(0)
  • 2020-12-13 12:23

    Even more numpy

    np.square(np.subtract(A, B)).mean()
    
    0 讨论(0)
  • 2020-12-13 12:23

    Just for kicks

    mse = (np.linalg.norm(A-B)**2)/len(A)

    0 讨论(0)
  • 2020-12-13 12:24

    Another alternative to the accepted answer that avoids any issues with matrix multiplication:

     def MSE(Y, YH):
         return np.square(Y - YH).mean()
    

    From the documents for np.square: "Return the element-wise square of the input."

    0 讨论(0)
  • 2020-12-13 12:38

    The standard numpy methods for calculation mean squared error (variance) and its square root (standard deviation) are numpy.var() and numpy.std(), see here and here. They apply to matrices and have the same syntax as numpy.mean().

    I suppose that the question and the preceding answers might have been posted before these functions became available.

    0 讨论(0)
提交回复
热议问题