簡體   English   中英

兩個4D numpy數組中所有行的組合

[英]Combination of all rows in two 4D numpy arrays

我有兩個名為my_arr1, my_arr2 numpy數組,它們的大小100x30x30x3 我想輸出第一個數組的每一行與第二個數組的每一行的組合,並輸出到大小為10000x30x30x3兩個新數組。 通過遵循以下示例,通過一個具有兩個4x2大小的數組的簡單示例,設法做到了:

a1_ = np.array([[1, 1],
           [2, 2],
           [3, 0],
           [4, 2],
           [5, 3]])
a2_ = np.array([[10, 1],
           [11, 2],
           [12, 0],
           [13, 2],
           [14, 3]])


def combine_matrices(a1, a2):

   m1, n1 = a1.shape
   m2, n2 = a2.shape
   out = np.zeros((m1, m2, n1+n2), dtype=int)
   out[:, :, :n1] = a1[:, None, :]
   out[:, :, n1:] = a2
   out.shape = (m1*m2, -1)

 return out

結果是一個25x4數組,可以將其拆分為兩個25x2數組。 如果a1 = np.zeros((100, 30, 30, 3))a2 = np.zeros((100. 30, 30, 3))具有最終作用域以返回兩個數組,則如何修改函數大小(10000, 30, 30 ,3) ,而不僅僅是一個。

您可以使用numpy.outer完成此操作

但是,您不僅可以使用該函數,而且可能會有更好的選擇,但是我認為您必須遍歷其他矩陣指標。

# Use transpose to make the empty array shape (10x30x30x1000)
out_arr = numpy.zeros(shape=(1000, 30, 30, 10)).transpose()

for i in range(out_arr.shape[0]):
   for j in range(out_arr.dtype[1]):
      for k in range(out_arr.dtype[2]):
          # Use transpose again to get the data you want 
          # (transposing first before grabbing the index) 
          out_arr[i][j][k] = numpy.outer(arr1.transpose()[i][j][k], arr2.transpose()[i][j][k])

# 1000x30x30x10 array (rather than 10x30x30x1000)
out_arr = out_arr.transpose()

我將轉置作為技巧,因為您說的數據是(1000,30,30,10)。

使我不滿意的一件事是犯了嘗試做的錯誤:

arr[:][i][j][k]

因為在這種情況下,這將無法獲取您想要的內存。 它獲取與以下數據相同的數據:

arr[i][j][k]

並非100%肯定這是您想要的,但是它能正確顯示形狀。

首先建立診斷輸入

>>> a1 = np.empty((2, 2, 2, 3, 4), 'U1')
>>> for i, x in enumerate(np.ogrid[:2, :2, :2, :3]):
...      a1[..., i] = x
... 
>>> a2 = a1 = a1.view('U4').reshape(a1.shape[:-1])
>>> a1
array([[[['0000', '0001', '0002'],
         ['0010', '0011', '0012']],

        [['0100', '0101', '0102'],
         ['0110', '0111', '0112']]],


       [[['1000', '1001', '1002'],
         ['1010', '1011', '1012']],

        [['1100', '1101', '1102'],
         ['1110', '1111', '1112']]]], dtype='<U4')

接下來,分配輸出

>>> A1, A2 = (np.empty((a.shape[0], *a.shape), a.dtype) for a in (a1, a2))

使用廣播填寫

>>> A1[...] = a1[:, None]
>>> A2[...] = a2[None]

合並前兩個軸

>>> A1, A2 = (A.reshape(-1, *A.shape[2:]) for A in (A1, A2))

完成

>>> A1
array([[[['0000', '0001', '0002'],
         ['0010', '0011', '0012']],

        [['0100', '0101', '0102'],
         ['0110', '0111', '0112']]],


       [[['0000', '0001', '0002'],
         ['0010', '0011', '0012']],

        [['0100', '0101', '0102'],
         ['0110', '0111', '0112']]],


       [[['1000', '1001', '1002'],
         ['1010', '1011', '1012']],

        [['1100', '1101', '1102'],
         ['1110', '1111', '1112']]],


       [[['1000', '1001', '1002'],
         ['1010', '1011', '1012']],

        [['1100', '1101', '1102'],
         ['1110', '1111', '1112']]]], dtype='<U4')
>>> A2
array([[[['0000', '0001', '0002'],
         ['0010', '0011', '0012']],

        [['0100', '0101', '0102'],
         ['0110', '0111', '0112']]],


       [[['1000', '1001', '1002'],
         ['1010', '1011', '1012']],

        [['1100', '1101', '1102'],
         ['1110', '1111', '1112']]],


       [[['0000', '0001', '0002'],
         ['0010', '0011', '0012']],

        [['0100', '0101', '0102'],
         ['0110', '0111', '0112']]],


       [[['1000', '1001', '1002'],
         ['1010', '1011', '1012']],

        [['1100', '1101', '1102'],
         ['1110', '1111', '1112']]]], dtype='<U4')

暫無
暫無

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

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