Numpy covariance matrix numpy.cov

I am using numpy and want to calculate the covariance matrix for ndarray. I am trying to use numpy.cov () but I am not getting correct results. See below for details.

My ndarray is 768x8, where 8 are the numbers in my dataset.

When I use MATLAB to calculate the covariance matrix I get 8x8 (which is what I need), but when I use np.cov () I get 768x768 which is not correct. I tried to change the rowvar argument to true and it doesn't work.

What would be the correct call to numpy.cov ()? In other words, how would I reproduce the cov () results from MATLAB using numpy.

+3


source to share


1 answer


It's amazing what the documentation has to tell you. You must pass rowvar=False

to indicate that the columns represent variables.



>>> data.shape
(768, 8)
>>> numpy.cov(data, rowvar=False).shape
(8, 8)

      

+9


source







All Articles