簡體   English   中英

二維 numpy 陣列的所有可能組合

[英]All possible combinations of 2D numpy array

我有四個 numpy arrays,示例如下:

a1=np.array([[-24.4925, 295.77  ],
             [-24.4925, 295.77  ],
             [-14.3925, 295.77  ],
             [-16.4125, 295.77  ],
             [-43.6825, 295.77  ],
             [-22.4725, 295.77  ]])

a2=np.array([[-26.0075, 309.39  ],
             [-24.9975, 309.39  ],
             [-14.8975, 309.39  ],
             [-17.9275, 309.39  ],
             [-46.2075, 309.39  ],
             [-23.9875, 309.39  ]])

a3=np.array([[-25.5025, 310.265 ],
             [-25.5025, 310.265 ],
             [-15.4025, 310.265 ],
             [-17.4225, 310.265 ],
             [-45.7025, 310.265 ],
             [-24.4925, 310.265 ]])

a4=np.array([[-27.0175, 326.895 ],
             [-27.0175, 326.895 ],
             [-15.9075, 326.895 ],
             [-18.9375, 326.895 ],
             [-48.2275, 326.895 ],
             [-24.9975, 326.895 ]])

我想在 arrays 之間進行所有可能的組合並同時連接,例如:

array[-24.4925, 295.77, -26.0075, 309.39, -25.5025, 310.265, -27.0175, 326.895]

array[-24.4925, 295.77, -26.0075, 309.39, -25.5025, 310.265, -27.0175, 326.895]

[a1[0],a2[0],a3[0],a4[0]] , [a1[0],a2[0],a3[0],a4[1]]等等

除了循環四個 arrays 之外,最快的方法是什么?!

好吧,沒有比循環更快的方法了,但是有一種干凈的方法,您不必編寫循環:

import numpy as np
import itertools

a1=np.array([[-24.4925, 295.77  ],
             [-24.4925, 295.77  ],
             [-14.3925, 295.77  ],
             [-16.4125, 295.77  ],
             [-43.6825, 295.77  ],
             [-22.4725, 295.77  ]])

a2=np.array([[-26.0075, 309.39  ],
             [-24.9975, 309.39  ],
             [-14.8975, 309.39  ],
             [-17.9275, 309.39  ],
             [-46.2075, 309.39  ],
             [-23.9875, 309.39  ]])

a3=np.array([[-25.5025, 310.265 ],
             [-25.5025, 310.265 ],
             [-15.4025, 310.265 ],
             [-17.4225, 310.265 ],
             [-45.7025, 310.265 ],
             [-24.4925, 310.265 ]])

a4=np.array([[-27.0175, 326.895 ],
             [-27.0175, 326.895 ],
             [-15.9075, 326.895 ],
             [-18.9375, 326.895 ],
             [-48.2275, 326.895 ],
             [-24.9975, 326.895 ]])

arrays = [a1, a2, a3, a4]

for pieces in itertools.product(*arrays):
    combined = np.concatenate(pieces, axis = 0)
    print(combined)

標准庫itertools模塊( https://docs.python.org/3/library/itertools.html )提供了各種用於生產迭代產品的工具。 由於 numpy 數組恰好是可迭代的(迭代第一個索引),我們可以使用itertools從每個數組中獲取切片,然后使用 numpy 組合它們。

這是一個numpy解決方案,基於此處的笛卡爾積實現。

arr = np.stack([a1, a2, a3, a4])

print(arr.shape) # (4, 6, 2)
n, m, k = arr.shape

# from https://stackoverflow.com/questions/11144513/cartesian-product-of-x-and-y-array-points-into-single-array-of-2d-points
def cartesian_product(*arrays):
    la = len(arrays)
    dtype = np.result_type(*arrays)
    arr = np.empty([len(a) for a in arrays] + [la], dtype=dtype)
    for i, a in enumerate(np.ix_(*arrays)):
        arr[...,i] = a
    return arr.reshape(-1, la)

inds = cartesian_product(*([np.arange(m)] * n))
res = np.take_along_axis(arr, inds.T[...,None], 1).swapaxes(0,1).reshape(-1, n*k)

print(res[0])
# [-24.4925 295.77   -26.0075 309.39   -25.5025 310.265  -27.0175 326.895 ]

在此示例中, inds數組如下所示:

print(inds[:10])
# [[0 0 0 0]
#  [0 0 0 1]
#  [0 0 0 2]
#  [0 0 0 3]
#  [0 0 0 4]
#  [0 0 0 5]
#  [0 0 1 0]
#  [0 0 1 1]
#  [0 0 1 2]
#  [0 0 1 3]]

然后我們可以為每個組合使用np.take_along_axis到 select 適當的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM