如何以50%的几率沿x轴翻转3D数据?

2024-04-23 10:45:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我读了一篇论文,他们提到

flipped data with 50% chance along x-axis.

给定输入数据为40x40x24。如何执行上述要求?我正在使用Python2.7尝试下面的代码,但我不确定“50%的可能性”的含义

data_flip = np.flipud(data)
data_flip = data[:, ::-1, :]

Tags: 数据代码datawithnp可能性含义axis
1条回答
网友
1楼 · 发布于 2024-04-23 10:45:50

首先,为了从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]) 

相关问题 更多 >