协方差矩阵的对角元素不是1 pandas/numpy [英] Diagonal element for covariance matrix not 1 pandas/numpy

查看:277
本文介绍了协方差矩阵的对角元素不是1 pandas/numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

   A  B
0  1  5
1  2  6
2  3  7
3  4  8

我希望计算协方差

a = df.iloc[:,0].values

b = df.iloc[:,1].values

使用numpy作为cov:

Using numpy for cov as :

numpy.cov(a,b)

我得到:

array([[ 1.66666667,  1.66666667],
   [ 1.66666667,  1.66666667]])

对角线元素不是1吗?如何将对角线元素设为1?

Shouldn't the diagonal elements be 1? How do I get the diagonal elements to 1?

推荐答案

不,他们不应该这样做.我认为您可能会将它与相关性"混淆. 相关性和协方差是不同的.

No they shouldn't. I think you might be confusing it with Correlation. Correlation and Covariance are different.

在对角线上看到的只是变量的方差! 公式的Wiki屏幕截图-

What you see in the diagonals is simply the variance of the variables! Wiki screenshot for the formulas -

Wiki链接

这篇关于协方差矩阵的对角元素不是1 pandas/numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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