【问题标题】:Draw multiple samples using numpy.random.multivariate_normal(mean, cov[, size])使用 numpy.random.multivariate_normal(mean, cov[, size]) 绘制多个样本
【发布时间】:2016-04-28 05:09:13
【问题描述】:

使用 numpy 函数numpy.random.multivariate_normal(),如果我给出均值和协方差,我可以从多元高斯中抽取随机样本。

举个例子,

import numpy as np
mean = np.zeros(1000)  # a zero array shaped (1000,)
covariance = np.random.rand(1000, 1000) 
# a matrix of random values shaped (1000,1000)
draw = np.random.multivariate_normal(mean, covariance)
# this outputs one "draw" of a multivariate norm, shaped (1000,)

上述函数从多元高斯输出一个“绘制”,形状为(1000,)(因为协方差矩阵的形状为1000,1000))。

我想要 200 次抽奖。如何做到这一点?我会创建一个列表理解,但我看不到如何创建迭代。

编辑:有没有区别

draw_A = np.random.rand(1000, 1000, 200)

draw_B = [np.random.multivariate_normal(mean, covariance) for i in range(200)]

?

是的,draw_B 是一个列表,但它们都是 200 个独立绘制形状的 1000,1000)

【问题讨论】:

    标签: python arrays numpy random


    【解决方案1】:

    您注意到docstring 中的size 参数了吗?

    例如,此调用从 3-d 分布生成 5 个样本:

    In [22]: np.random.multivariate_normal(np.zeros(3), np.eye(3), size=5)
    Out[22]: 
    array([[ 1.08534253,  0.70492174, -0.8625333 ],
           [ 0.16955737, -0.89453284,  0.8347796 ],
           [ 0.49506717, -1.18087912, -0.89118919],
           [-0.97837406, -0.42304268,  0.4326744 ],
           [-1.18836816,  1.33389231,  0.23616035]])
    

    对已编辑问题的回复:

    • np.random.rand(d0, d1, d2) 使 d0*d1*d2 从 [0, 1) 上的 univariate uniform 分布中随机抽取,并将它们返回到形状为 (d0, d1, d2) 的数组中。
    • np.random.multivariate_normal(mean, cov, size=n),其中mean 是一个形状为(m,) 的数组,cov 是一个形状为(m, m) 的数组,使n 从多元正态分布中绘制,并将它们作为数组的行返回形状(n, m)。您的列表理解draw_B 还从多元正态分布中抽取样本,每个函数调用一个样本,并将样本放入列表而不是数组中。

    【讨论】:

    • 是的,我注意到了这一点。请参阅我上面的编辑以进行澄清。 draw_Adraw_B 一样吗?
    猜你喜欢
    • 2013-02-17
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多