numpy.cov()返回意外的输出 [英] numpy.cov() returns unexpected output

查看:177
本文介绍了numpy.cov()返回意外的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个X数据集,其中包含9个要素和683行(683x9).我想要这个X数据集和另一个与X具有相同形状的数据集的协方差矩阵.我使用np.cov(originalData, generatedData, rowvar=False)代码获取它,但它返回了形状为18x18的协方差矩阵.我期望得到9x9的协方差矩阵.您能帮我解决这个问题吗?

I have a X dataset which has 9 features and 683 rows (683x9). I want to take covariance matrix of this X dataset and another dataset which has same shape with X. I use np.cov(originalData, generatedData, rowvar=False) code to get it but it returns a covariance matrix of shape 18x18. I expected to get 9x9 covariance matrix. Can you please help me to fix it.

推荐答案

方法cov计算给定变量的所有对的协方差.在一个数组中有9个变量,在另一个数组中有9个变量.总共18个.因此,您得到18 x 18矩阵. (在内部,cov在计算协方差之前将您给它的两个数组连接在一起).

The method cov calculates the covariances for all pairs of variables that you give it. You have 9 variables in one array, and 9 more in the other. That's 18 in total. So you get 18 by 18 matrix. (Under the hood, cov concatenates the two arrays you gave it before calculating the covariance).

如果您只对第一个数组中的变量与第二个数组中的变量的协方差感兴趣,请选择行的前半部分和列的后半部分:

If you are only interested in the covariance of the variables from the 1st array with the variables from the 2nd, pick the first half of rows and second half of columns:

C = np.cov(originalData, generatedData, rowvar=False)[:9, 9:]

或者通常,两个不一定相等的矩阵X和Y,

Or in general, with two not necessarily equal matrices X and Y,

C = np.cov(X, Y, rowvar=False)[:X.shape[1], Y.shape[1]:]

这篇关于numpy.cov()返回意外的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆