Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Covariance Matrix numpy.cov

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

My ndarray is 768x8 for where 8 is the numbers features in my data set.

When I use MATLAB to compute the covariance matrix, I get a 8x8 (which is what I require), but when I use np.cov(), I get a 768x768 which is incorrect. I tried changing the rowvar argument to true and this does not work.

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

like image 810
user152945 Avatar asked Dec 03 '22 22:12

user152945


2 Answers

Amazingly, the documentation might tell you. You should pass rowvar=False to indicate that columns represent variables.

>>> data.shape
(768, 8)
>>> numpy.cov(data, rowvar=False).shape
(8, 8)
like image 89
donkopotamus Avatar answered Dec 11 '22 17:12

donkopotamus


as per default, each row is observation, each column is feature, which is swaped in numpi per definition, so all you need to do is transpose, where R is matrix

np.cov(R.T) 

or

np.cov(R, rowvar = False)
like image 41
Sherif O. Avatar answered Dec 11 '22 17:12

Sherif O.