我有一個 3D 陣列,我想從第 1 軸 N 次獲取隨機“集合”(注意:不是 pythonic 集合)。我可以通過嵌套的 For 回圈來實作這一點,但我需要這樣做至少 10000 次,所以如果可能的話,我需要找到一個矢量化的解決方案。
我將嘗試用一個例子來解釋這一點。如果我想檢索 N 組資料,我想從我的 3D 陣列中的軸 1 中為軸 0 中的每個元素選擇一個隨機索引。例如,在我的 N 組中的第一個隨機選擇索引[0, 2, 1],這與三個相關不同的陣列位置:[0, 0, :]、[1, 2, :]、 和[2, 1, :],分別(即軸 0 每次遞增 1,軸 1 基于隨機選擇的索引)。
以下是偽代碼中的數字示例:
# Create some arbitrary data (EDIT: based on mozway's answer)
a = array([[[ 0. , 4. , 8. , 12. , 16. , 20. , 24. ],
[ 1. , 5. , 9. , 13. , 17. , 21. , 25. ],
[ 2. , 6. , 10. , 14. , 18. , 22. , 26. ],
[ 3. , 7. , 11. , 15. , 19. , 23. , 27. ]],
[[ 0.1, 4.1, 8.1, 12.1, 16.1, 20.1, 24.1],
[ 1.1, 5.1, 9.1, 13.1, 17.1, 21.1, 25.1],
[ 2.1, 6.1, 10.1, 14.1, 18.1, 22.1, 26.1],
[ 3.1, 7.1, 11.1, 15.1, 19.1, 23.1, 27.1]],
[[ 0.2, 4.2, 8.2, 12.2, 16.2, 20.2, 24.2],
[ 1.2, 5.2, 9.2, 13.2, 17.2, 21.2, 25.2],
[ 2.2, 6.2, 10.2, 14.2, 18.2, 22.2, 26.2],
[ 3.2, 7.2, 11.2, 15.2, 19.2, 23.2, 27.2]]])
# Define the number of requested sets
N = 2
# Define the chosen data per 'set' (normally would be random)
idx = [[0, 2, 1], [1, 3, 3]]
# First set would give (with choices [0, 2, 1]):
arr = [[ 0. , 4. , 8. , 12. , 16. , 20. , 24. ],
[ 2.1, 6.1, 10.1, 14.1, 18.1, 22.1, 26.1],
[ 1.2, 5.2, 9.2 , 13.2, 17.2, 21.2, 25.2]]
# Second set would give (with choices [1, 3, 3]):
arr = [[ 1. , 5. , 9. , 13. , 17. , 21. , 25. ],
[ 3.1, 7.1, 11.1, 15.1, 19.1, 23.1, 27.1],
[ 3.2, 7.2, 11.2, 15.2, 19.2, 23.2, 27.2]]
# So, the final output would combine all sets:
arr = [[[ 0. , 4. , 8. , 12. , 16. , 20. , 24. ],
[ 2.1, 6.1, 10.1, 14.1, 18.1, 22.1, 26.1],
[ 1.2, 5.2, 9.2 , 13.2, 17.2, 21.2, 25.2]],
[ 1. , 5. , 9. , 13. , 17. , 21. , 25. ],
[ 3.1, 7.1, 11.1, 15.1, 19.1, 23.1, 27.1],
[ 3.2, 7.2, 11.2, 15.2, 19.2, 23.2, 27.2]]]
uj5u.com熱心網友回復:
澄清問題前的原始答案,請參閱獨立抽樣的新答案
您可以獲得隨機索引和切片:
N = 2
# get random indices on the first dimension
idx = np.random.choice(np.arange(x.shape[0]), size=N)
# slice
x[idx]
示例輸出(形狀:(2、3、7)):
array([[[ 1, 2, 5, 10, 17, 26, 37],
[ 2, 3, 6, 11, 18, 27, 38],
[ 3, 4, 7, 12, 19, 28, 39],
[ 4, 5, 8, 13, 20, 29, 40]],
[[ 1, 2, 3, 4, 5, 6, 7],
[ 2, 3, 4, 5, 6, 7, 8],
[ 3, 4, 5, 6, 7, 8, 9],
[ 4, 5, 6, 7, 8, 9, 10]]])
其他維度的示例:
# second dimension (axis 1)
idx = np.random.choice(np.arange(x.shape[1]), size=N)
x[:, idx]
uj5u.com熱心網友回復:
鑒于您的問題的澄清,您希望在軸 1(第二維)上的 3D 陣列中選擇 N 個隨機行,但在軸 0 上獨立:
我們將 a 稱為陣列,并將 x,y,z 稱為它的 3 個維度。
一個簡單的方法是選擇 N*x 個隨機索引,這樣每個 x 就有 N 個。然后在前 2 個維度上展平陣列并切片。
示例輸入(注意 x/x.1/x.2 以跟蹤原始維度):
array([[[ 0. , 4. , 8. , 12. , 16. , 20. , 24. ],
[ 1. , 5. , 9. , 13. , 17. , 21. , 25. ],
[ 2. , 6. , 10. , 14. , 18. , 22. , 26. ],
[ 3. , 7. , 11. , 15. , 19. , 23. , 27. ]],
[[ 0.1, 4.1, 8.1, 12.1, 16.1, 20.1, 24.1],
[ 1.1, 5.1, 9.1, 13.1, 17.1, 21.1, 25.1],
[ 2.1, 6.1, 10.1, 14.1, 18.1, 22.1, 26.1],
[ 3.1, 7.1, 11.1, 15.1, 19.1, 23.1, 27.1]],
[[ 0.2, 4.2, 8.2, 12.2, 16.2, 20.2, 24.2],
[ 1.2, 5.2, 9.2, 13.2, 17.2, 21.2, 25.2],
[ 2.2, 6.2, 10.2, 14.2, 18.2, 22.2, 26.2],
[ 3.2, 7.2, 11.2, 15.2, 19.2, 23.2, 27.2]]])
加工:
N = 2
# sample with repeats
idx = np.random.randint(y, size=N*x)
corr = np.repeat(np.arange(0,(x-1)*y 1, y), N)
idx = corr
# sample without repeats
idx = np.concatenate([np.random.choice(list(range(y)), replace=False, size=N) (i*y) for i in range(x)])
# slice array
a.reshape(x*y,z)[idx].reshape(x,N,z).swapaxes(0,1)
可能的輸出 (N,x,z) 形狀:
array([[[ 0. , 4. , 8. , 12. , 16. , 20. , 24. ],
[ 1.1, 5.1, 9.1, 13.1, 17.1, 21.1, 25.1],
[ 0.2, 4.2, 8.2, 12.2, 16.2, 20.2, 24.2]],
[[ 3. , 7. , 11. , 15. , 19. , 23. , 27. ],
[ 3.1, 7.1, 11.1, 15.1, 19.1, 23.1, 27.1],
[ 1.2, 5.2, 9.2, 13.2, 17.2, 21.2, 25.2]]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/316927.html
