2017-05-05 74 views
0

我讀一個紙和它們提及作爲如何以50%的概率沿x軸翻轉3D數據?

與沿x軸50%的機會翻轉數據。

鑑於輸入數據是40x40x24。我如何執行上述要求?我使用Python 2.7版試圖波紋管代碼,但我不知道關於「50%的機會」,這意味着

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

回答

1

首先,爲了選擇出的概率n元素p,你可以簡單地使用的:np.random.rand(n) < pr = np.random.rand()[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]) 
+0

感謝您的回答。你能檢查最後一行'AA [:,:,idxs] = np.fliplr(AA [:,:,idxs])''。是A,而不是AA?另外,你能解釋一下'50%機率'的含義嗎? – user8264

+0

'AA'應該是'A' - 謝謝,修正了這個問題。我也闡述了50%的機會。附加代碼中的前3行只是爲了生成一些數據來處理(您已經擁有)。 – user2999345

+0

啊我明白了,對不起。這個想法是隨機選擇一個概率爲0.5的切片並將其翻轉。非常感謝 – user8264