Search code examples
python-3.xnumpycovariance

Python3 np.cov giving strange result


import numpy as np    

X = np.arange(6).reshape(2,3)
np.cov(X)

#Results in:
array([[1., 1.],
       [1., 1.]])

While it should output:

array([[0.66666667, 0.66666667],
       [0.66666667, 0.66666667]])

Solution

  • As mentioned in comments, the default cov is normalized by N-1 which is an unbiased estimate. To get simple average you can do either of these solutions:

    np.cov(X,bias=True)
    

    or

    np.cov(X,ddof=0)
    

    output:

    [[0.66666667 0.66666667]
     [0.66666667 0.66666667]]