2

在numpy手册中,据说:

Instead of specifying the full covariance matrix, popular approximations include:  
    Spherical covariance (cov is a multiple of the identity matrix)

有没有人指定球面协方差?我正在努力避免构建完整的协方差矩阵,这会占用太多内存。

4

2 回答 2

1

虽然@RobertKern 的方法是正确的,但您可以让 numpy 为您处理所有这些,就像np.random.normal以多种方式和标准偏差进行广播一样:

>>> np.random.normal(0, [1,2,3])
array([ 0.83227999,  3.40954682, -0.01883329])

要获得多个随机样本,您必须给它一个适当的大小:

>>> x = np.random.normal(0, [1, 2, 3], size=(1000, 3))
>>> np.std(x, axis=0)
array([ 1.00034817,  2.07868385,  3.05475583])
于 2013-05-16T15:36:49.567 回答
1

如果您只有一个对角协方差矩阵,通常更容易(也更有效)自己缩放标准正态变量而不是使用multivariate_normal().

>>> import numpy as np
>>> stdevs = np.array([3.0, 4.0, 5.0])
>>> x = np.random.standard_normal([100, 3])
>>> x.shape
(100, 3)
>>> x *= stdevs
>>> x.std(axis=0)
array([ 3.23973255,  3.40988788,  4.4843039 ])
于 2013-05-16T09:31:58.840 回答