簡體   English   中英

如何延長 numpy 數組的最后一個維度並用另一個數組填充它?

[英]How to length the last dimension of a numpy array and fill it up with another array?

我有一個形狀為(5, 4, 3)的 numpy 數組和另一個形狀為(4,)的 numpy 數組,我想做的是擴展第一個數組的最后一個維度(5, 4, 3) -> (5, 4, 4)然后廣播具有形狀(4,)的另一個數組,使其分別填滿新的數組單元。

例子:

np.ones((5,4,3))
array([[[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]])

變成

array([[[1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.]],

       [[1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.]],

       [[1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.]],

       [[1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.]],

       [[1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.],
        [1., 1., 1., 0.]]])

然后我有另一個數組

array([2., 3., 4., 5.])

我以某種方式用第一個廣播來填充零:

array([[[1., 1., 1., 2.],
        [1., 1., 1., 3.],
        [1., 1., 1., 4.],
        [1., 1., 1., 5.]],

       [[1., 1., 1., 2.],
        [1., 1., 1., 3.],
        [1., 1., 1., 4.],
        [1., 1., 1., 5.]],

       [[1., 1., 1., 2.],
        [1., 1., 1., 3.],
        [1., 1., 1., 4.],
        [1., 1., 1., 5.]],

       [[1., 1., 1., 2.],
        [1., 1., 1., 3.],
        [1., 1., 1., 4.],
        [1., 1., 1., 5.]],

       [[1., 1., 1., 2.],
        [1., 1., 1., 3.],
        [1., 1., 1., 4.],
        [1., 1., 1., 5.]]])

我怎樣才能做到這一點?

您可以使用numpy.c_numpy.tile

A = np.ones((5,4,3), dtype='int')
B = np.array([2, 3, 4, 5])

np.c_[A, np.tile(B[:,None], (A.shape[0], 1, 1))]

output:

array([[[1, 1, 1, 2],
        [1, 1, 1, 3],
        [1, 1, 1, 4],
        [1, 1, 1, 5]],

       [[1, 1, 1, 2],
        [1, 1, 1, 3],
        [1, 1, 1, 4],
        [1, 1, 1, 5]],

       [[1, 1, 1, 2],
        [1, 1, 1, 3],
        [1, 1, 1, 4],
        [1, 1, 1, 5]],

       [[1, 1, 1, 2],
        [1, 1, 1, 3],
        [1, 1, 1, 4],
        [1, 1, 1, 5]],

       [[1, 1, 1, 2],
        [1, 1, 1, 3],
        [1, 1, 1, 4],
        [1, 1, 1, 5]]])

這個怎么運作:

# reshape B to add one dimension
>>> B[:, None]
array([[2],
       [3],
       [4],
       [5]])

# tile to match A's first dimension
>>> np.tile(B[:,None], (A.shape[0], 1, 1))
array([[[2],
        [3],
        [4],
        [5]],

       [[2],
        [3],
        [4],
        [5]],

       [[2],
        [3],
        [4],
        [5]],

       [[2],
        [3],
        [4],
        [5]],

       [[2],
        [3],
        [4],
        [5]]])

您可以使用numpy.append

A=np.ones((5,4,3))
AA=np.zeros((5,4,1))
B=np.array([2., 3., 4., 5.])
C=np.append(A,AA, axis=2)
for i in range(np.shape(C)[0]):
    for j in range(np.shape(C)[1]):
            C[i,j,-1]=B[j]
print(C)

> 
[[[1. 1. 1. 2.]
  [1. 1. 1. 3.]
  [1. 1. 1. 4.]
  [1. 1. 1. 5.]]

 [[1. 1. 1. 2.]
  [1. 1. 1. 3.]
  [1. 1. 1. 4.]
  [1. 1. 1. 5.]]

 [[1. 1. 1. 2.]
  [1. 1. 1. 3.]
  [1. 1. 1. 4.]
  [1. 1. 1. 5.]]

 [[1. 1. 1. 2.]
  [1. 1. 1. 3.]
  [1. 1. 1. 4.]
  [1. 1. 1. 5.]]

 [[1. 1. 1. 2.]
  [1. 1. 1. 3.]
  [1. 1. 1. 4.]
  [1. 1. 1. 5.]]]

有多種方法可以做到這一點,但最簡單的一種是制作所需最終大小的數組,並填寫值。

我可以從np.zeros((5,4,4))開始,然后插入np.ones((5,4,3)) ,但為什么不從所有的開始:

In [680]: res = np.ones((5,4,4))

我們可以輕松地將 4 元素列表/數組復制到最后一列:

In [681]: res[:,:,-1] = [2,3,4,5]
In [682]: res
Out[682]: 
array([[[1., 1., 1., 2.],
        [1., 1., 1., 3.],
        [1., 1., 1., 4.],
        [1., 1., 1., 5.]],

       [[1., 1., 1., 2.],
        [1., 1., 1., 3.],
        [1., 1., 1., 4.],
        [1., 1., 1., 5.]],
       ...

(4,) 形狀數組被broadcasted到 (1,4),它很容易適合res[:,:,-1]定義的(5,4)槽。

將 (4,) 擴展為 (5,4,1) (使用tile ),然后將其與 (5,4,3) 連接也可以。

暫無
暫無

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

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