np.cov 给出了意外数量的值
np.cov giving unexpected number of values
在包含 10 个值的随机数据集上使用 np.cov
命令时,我得到一个 10x10
数组作为答案。我认为我的数据格式不正确,但我不确定。
np.random.seed(1)
rho = 0.2
sigma = 1
cov = (sigma**2)*[[1,rho],[rho,1]]
mean1 = (0,0)
x1 = np.random.multivariate_normal(mean1, cov, (10))
mean1 = np.mean(x1)
cov1 = np.cov(x1)
print(cov1)
这是正确的行为——np.cov
returns 协方差矩阵。
特别是,它将输入的每一行作为一个变量,列代表这些变量的不同值。要逆转此行为,请传递 rowvar=False
.
特别是,如果您有两个变量表示为矩阵的两列,则可以使用 np.cov(data, rowvar=False)
(或 np.cov(data.T)
)获得 2 x 2 协方差矩阵,其中元素cov[0,1]
和 cov[1,0]
将是两个变量之间的协方差。
这也被讨论here。
在包含 10 个值的随机数据集上使用 np.cov
命令时,我得到一个 10x10
数组作为答案。我认为我的数据格式不正确,但我不确定。
np.random.seed(1)
rho = 0.2
sigma = 1
cov = (sigma**2)*[[1,rho],[rho,1]]
mean1 = (0,0)
x1 = np.random.multivariate_normal(mean1, cov, (10))
mean1 = np.mean(x1)
cov1 = np.cov(x1)
print(cov1)
这是正确的行为——np.cov
returns 协方差矩阵。
特别是,它将输入的每一行作为一个变量,列代表这些变量的不同值。要逆转此行为,请传递 rowvar=False
.
特别是,如果您有两个变量表示为矩阵的两列,则可以使用 np.cov(data, rowvar=False)
(或 np.cov(data.T)
)获得 2 x 2 协方差矩阵,其中元素cov[0,1]
和 cov[1,0]
将是两个变量之间的协方差。
这也被讨论here。