首先,为了从n元素中选择概率为p的元素,您可以简单地使用:np.random.rand(n) < p^{}根据[0, 1)上的均匀分布生成一个数,因此r小于某个常数p(其中p在[0,1]中)的概率正好是p。这个概率实际上是CDF of the distribution,在这种情况下,a=0和b=1是:
F(p) = 0, p<0
p, 0<=p<=1
1, p>1
其次,要沿x轴翻转数据,请使用np.fliplr而不是np.flipud(沿y轴翻转):
# generate a 3D array size 3x3x5
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
A = np.tile( np.expand_dims(A, axis=2), (1,1,5) )
# index the 3rd axis with probability 0.5
p = 0.5
idxs = np.random.rand(A.shape[2]) < p
# flip left-right the chosen arrays in the 3rd dimension
A[:,:,idxs] = np.fliplr(A[:,:,idxs])
首先,为了从} 根据
n
元素中选择概率为p
的元素,您可以简单地使用:np.random.rand(n) < p
^{[0, 1)
上的均匀分布生成一个数,因此r
小于某个常数p
(其中p
在[0,1]中)的概率正好是p
。这个概率实际上是CDF of the distribution,在这种情况下,a=0和b=1是:其次,要沿x轴翻转数据,请使用
np.fliplr
而不是np.flipud
(沿y轴翻转):相关问题 更多 >
编程相关推荐