numpy.random.multivariate_normal(mean,cov [,size])

时间:2013-03-02 23:11:12

标签: python numpy covariance

numpy.random.multivariate_normal(mean,cov [,size])

我有一个N点和X维度的数据集。因此,当我计算numpy.mean(data,axis = 0)和numpy.cov(data)并使用numpy.random.multivariate_normal(mean,cov)中的mean和cov值时。它抛出以下错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 3986, in mtrand.RandomState.multivariate_normal (numpy/random/mtrand/mtrand.c:16833)
ValueError: mean and cov must have same length

因为numpy.mean()计算平均列,并给出一个X维数组。 而numpy.cov()的输出是具有N行和X列的协方差矩阵。 任何人都可以建议修复。

1 个答案:

答案 0 :(得分:3)

这与numpy.cov如何解释其第一个参数有关。您可以连续进行每次观察,而numpy.cov expects each observation in a column.

要解决此问题,请在np.cov(data.T)中转换数据以获取X x X协方差矩阵:

In [58]: N, X = 100, 3

In [59]: data = np.random.random((N,X))

In [60]: mean = np.mean(data, axis = 0)

In [61]: mean
Out[61]: array([ 0.4913433 ,  0.49484566,  0.52463666])

In [62]: np.cov(data.T).shape
Out[62]: (3, 3)

In [63]: cov = np.cov(data.T)

In [64]: np.random.multivariate_normal(mean, cov)
Out[64]: array([ 0.27194062,  0.65995531,  0.67367201])

或者,使用rowval=False参数:

In [68]: cov = np.cov(data, rowvar=False)

In [69]: cov.shape
Out[69]: (3, 3)