标签:
matlab是标准的,numpy相当于转置后计算
>> x = [2,0,-1.4;2.2,0.2,-1.5;2.4,0.1,-1;1.9,0,-1.2]
x =
2.0000 0 -1.4000
2.2000 0.2000 -1.5000
2.4000 0.1000 -1.0000
1.9000 0 -1.2000
>> cov(x)
ans =
0.0492 0.0142 0.0192
0.0142 0.0092 -0.0058
0.0192 -0.0058 0.0492
>> xt=x‘
xt =
2.0000 2.2000 2.4000 1.9000
0 0.2000 0.1000 0
-1.4000 -1.5000 -1.0000 -1.2000
>> cov(xt)
ans =
2.9200 3.1600 2.9500 2.6700
3.1600 3.4300 3.1750 2.8850
2.9500 3.1750 3.0100 2.7050
2.6700 2.8850 2.7050 2.4433
np.array(x).T
array([[ 2. , 2.2, 2.4, 1.9],
[ 0. , 0.2, 0.1, 0. ],
[-1.4, -1.5, -1. , -1.2]])
>>> print(np.cov(np.array(x).T))
[[ 0.04916667 0.01416667 0.01916667]
[ 0.01416667 0.00916667 -0.00583333]
[ 0.01916667 -0.00583333 0.04916667]]
np.array(x)
array([[ 2. , 0. , -1.4],
[ 2.2, 0.2, -1.5],
[ 2.4, 0.1, -1. ],
[ 1.9, 0. , -1.2]])
>>> print(np.cov(np.array(x)))
[[ 2.92 3.16 2.95 2.67 ]
[ 3.16 3.43 3.175 2.885 ]
[ 2.95 3.175 3.01 2.705 ]
[ 2.67 2.885 2.705 2.44333333]]
numpy和matlab计算协方差矩阵的不同(matlab是标准的,numpy相当于转置后计算)
标签:
原文地址:http://www.cnblogs.com/qqhfeng/p/5294118.html