如何沿 x 轴以 50% 的几率翻转 3D 数据?
How to flipped 3D data with 50% chance along x axis?
我读了一篇论文,他们提到了
flipped data with 50% chance along x-axis.
给定输入数据为 40x40x24。我怎样才能执行上述要求?我正在尝试使用 python 2.7 的波纹管代码,但我不确定“50% 机会”的含义
data_flip = np.flipud(data)
data_flip = data[:, ::-1, :]
首先,要以 p
的概率从 n
个元素中选出,您可以简单地使用:np.random.rand(n) < p
。 r = np.random.rand()
generates a number from a uniform distribution over [0, 1)
, so the probability that r
is smaller than some constant p
(where p
is in [0,1]) is exactly p
. This probability is actually the 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])
我读了一篇论文,他们提到了
flipped data with 50% chance along x-axis.
给定输入数据为 40x40x24。我怎样才能执行上述要求?我正在尝试使用 python 2.7 的波纹管代码,但我不确定“50% 机会”的含义
data_flip = np.flipud(data)
data_flip = data[:, ::-1, :]
首先,要以 p
的概率从 n
个元素中选出,您可以简单地使用:np.random.rand(n) < p
。 r = np.random.rand()
generates a number from a uniform distribution over [0, 1)
, so the probability that r
is smaller than some constant p
(where p
is in [0,1]) is exactly p
. This probability is actually the 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])