gpt4 book ai didi

python - 从随机矩阵生成样本

转载 作者:行者123 更新时间:2023-11-30 22:58:49 25 4
gpt4 key购买 nike

假设我们有一个大小为 n*m 的随机矩阵 A。每个元素 A_ij 是伯努利分布的成功概率。

我想使用以下规则从 A 中抽取样本 z:

z_ij 由伯努利得出(A_ij)

有没有numpy函数支持这个?

编辑:诸如此类的操作

arr = numpy.random.random([10, 5])
f = lambda x: numpy.random.binomial(1, x)
sp = map(f, arr)

效率低下。有没有更快的方法?

最佳答案

您可以直接给出一个数组作为二项式分布的参数之一,例如:

import numpy as np
arr = np.random.random([10, 5])
sp = np.random.binomial(1, arr)
sp

给出

array([[0, 0, 0, 0, 0],       [1, 0, 0, 1, 1],       [1, 0, 1, 0, 0],       [0, 0, 0, 1, 0],       [0, 0, 0, 0, 1],       [0, 1, 0, 1, 0],       [0, 1, 1, 0, 0],       [0, 0, 0, 1, 1],       [0, 1, 0, 0, 0],       [1, 0, 0, 1, 0]])

关于python - 从随机矩阵生成样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36034725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com