Numpy Covariance Matrix numpy.cov

左心房为你撑大大i 提交于 2020-08-18 07:48:14

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/43174624/numpy-covariance-matrix-numpy-cov

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!