使用numpy时协方差矩阵的结果不同。

2024-10-01 17:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白为什么我在使用np.cov公司公司名称:

import numpy as np
from tabulate import tabulate

np.random.seed(1)

a = np.random.random((10,3))
b = a - np.mean(a,axis=0,keepdims=True)

print "np.cov(a.T):"
print tabulate(np.cov(a.T),floatfmt='.2f',tablefmt='grid')
print "\nnp.dot(b.T,b):"
print tabulate(np.dot(b.T,b),floatfmt='.2f',tablefmt='grid') 

输出:

^{pr2}$

知道为什么结果不同吗?纽比用什么公式计算协方差?

更新: 我没有规范化我上面的协方差方程,使用(N-1)在评论中建议解决了这个问题。 但是,我注意到在我的实际示例中,我使用np.cov公司. 似乎如果我用np.savetxt文件与np.保存,我得到了不同的结果:

import numpy as np
from tabulate import tabulate

a1 = np.load('d1.npy')[:,0:3]
print "np.loadtxt:"
print tabulate(a1,floatfmt='.20f',tablefmt='grid')

a2 = np.loadtxt('/d1.txt')[:,0:3]
print "\nload:"
print tabulate(a2,floatfmt='.20f',tablefmt='grid')

b1 = a1 - np.mean(a1,axis=0,keepdims=True)
b2 = a2 - np.mean(a2,axis=0,keepdims=True)

print "\nnp.cov(a1.T):"
print tabulate(np.cov(a1.T),floatfmt='.14f',tablefmt='grid')

print "\nnp.dot(b1.T,b):"
print tabulate(np.dot(b1.T,b1)/float(len(b1)-1),floatfmt='.14f',tablefmt='grid')

print "\nnp.cov(a2.T):"
print tabulate(np.cov(a2.T),floatfmt='.14f',tablefmt='grid')

print "\nnp.dot(b2.T,b):"
print   tabulate(np.dot(b2.T,b2)/float(len(b2)-1),floatfmt='.14f',tablefmt='grid')

输出:

np.loadtxt:
+------------------------    +------------------------    +------------------------+
| 0.12400000542402267456     | 0.29300001263618469238     | 0.08000000566244125366     |
+------------------------    +------------------------    +------------------------+
| 0.12300000339746475220     | 0.28900000452995300293     | 0.06900000572204589844     |
+------------------------    +------------------------    +------------------------+
| 0.13800001144409179688     | 0.28100001811981201172     | 0.07400000095367431641     |
+------------------------    +------------------------    +------------------------+
| 0.13300000131130218506     | 0.26200002431869506836     | 0.07700000703334808350     |
+------------------------    +------------------------    +------------------------+
| 0.13800001144409179688     | 0.27900001406669616699     | 0.06700000166893005371     |
+------------------------    +------------------------    +------------------------+
| 0.12900000810623168945     | 0.26100000739097595215     | 0.07700000703334808350     |
+------------------------    +------------------------    +------------------------+
| 0.12700000405311584473     | 0.25600001215934753418     | 0.07700000703334808350     |
+------------------------    +------------------------    +------------------------+
| 0.12700000405311584473     | 0.26200002431869506836     | 0.08200000226497650146     |
+------------------------    +------------------------    +------------------------+
| 0.12400000542402267456     | 0.28000000119209289551     | 0.07300000637769699097     |
+------------------------    +------------------------    +------------------------+
| 0.12600000202655792236     | 0.30200001597404479980     | 0.06599999964237213135     |
+------------------------    +------------------------    +------------------------+

np.load:
+------------------------    +------------------------    +------------------------+
| 0.12400000542402267456     | 0.29300001263618469238     | 0.08000000566244125366     |
+------------------------    +------------------------    +------------------------+
| 0.12300000339746475220     | 0.28900000452995300293     | 0.06900000572204589844     |
+------------------------    +------------------------    +------------------------+
| 0.13800001144409179688     | 0.28100001811981201172     | 0.07400000095367431641     |
+------------------------    +------------------------    +------------------------+
| 0.13300000131130218506     | 0.26200002431869506836     | 0.07700000703334808350     |
+------------------------    +------------------------    +------------------------+
| 0.13800001144409179688     | 0.27900001406669616699     | 0.06700000166893005371     |
+------------------------    +------------------------    +------------------------+
| 0.12900000810623168945     | 0.26100000739097595215     | 0.07700000703334808350     |
+------------------------    +------------------------    +------------------------+
| 0.12700000405311584473     | 0.25600001215934753418     | 0.07700000703334808350     |
+------------------------    +------------------------    +------------------------+
| 0.12700000405311584473     | 0.26200002431869506836     | 0.08200000226497650146     |
+------------------------    +------------------------    +------------------------+
| 0.12400000542402267456     | 0.28000000119209289551     | 0.07300000637769699097     |
+------------------------    +------------------------    +------------------------+
| 0.12600000202655792236     | 0.30200001597404479980     | 0.06599999964237213135     |
+------------------------    +------------------------    +------------------------+

np.cov(a1.T):
+-------------------    +-------------------    +-------------------+
|  0.00003121113778     | -0.00001961109117     | -0.00000486667563     |
+-------------------    +-------------------    +-------------------+
| -0.00001961109117     |  0.00024427771650     | -0.00005066667516     |
+-------------------    +-------------------    +-------------------+
| -0.00000486667563     | -0.00005066667516     |  0.00002951112509     |
+-------------------    +-------------------    +-------------------+

np.dot(b1.T,b):
+-------------------    +-------------------    +-------------------+
|  0.00003121113696     | -0.00001961109228     | -0.00000486667523     |
+-------------------    +-------------------    +-------------------+
| -0.00001961109228     |  0.00024427770404     | -0.00005066667654     |
+-------------------    +-------------------    +-------------------+
| -0.00000486667523     | -0.00005066667654     |  0.00002951112219     |
+-------------------    +-------------------    +-------------------+

np.cov(a2.T):
+-------------------    +-------------------    +-------------------+
|  0.00003121113778     | -0.00001961109117     | -0.00000486667563     |
+-------------------    +-------------------    +-------------------+
| -0.00001961109117     |  0.00024427771650     | -0.00005066667516     |
+-------------------    +-------------------    +-------------------+
| -0.00000486667563     | -0.00005066667516     |  0.00002951112509     |
+-------------------    +-------------------    +-------------------+

np.dot(b2.T,b):
+-------------------    +-------------------    +-------------------+
|  0.00003121113778     | -0.00001961109117     | -0.00000486667563     |
+-------------------    +-------------------    +-------------------+
| -0.00001961109117     |  0.00024427771650     | -0.00005066667516     |
+-------------------    +-------------------    +-------------------+
| -0.00000486667563     | -0.00005066667516     |  0.00002951112509     |

如您所见,np.dot(b1.T,b)的结果略有不同。


Tags: importa2a1np公司covb2dot

热门问题