簡體   English   中英

如何將數組連接到 numpy 中 nd 數組的第一個 position?

[英]How to concatenate an array into first position of n-d array in numpy?

我需要在 numpy 的 nd 數組的第一個 position 中添加額外的元素。

這是代碼:

tmp_boxes3d = [None,6]

tmp_scores = [None]

現在我需要將 tmp_scores 元素添加到 tmp_boxes3d 的第一個 position 中。

為了更好地理解

boxes = np.array([[1,2,3,4],[5,6,7,8]])
scores = np.array([0,0])

boxes_scores = [[0,1,2,3,4],[0,5,6,7,8]]  # result

所以我需要為 [None,6] 形狀做。

誰能幫我這個?

嘗試np.concatenate

boxes = np.array([[1,2,3,4],[5,6,7,8]])
scores = np.array([0,0])

np.concatenate((scores[:, np.newaxis],  boxes), axis=1)
array([[0, 1, 2, 3, 4],
       [0, 5, 6, 7, 8]])

或者, np.hstack

np.hstack((scores[:, np.newaxis],  boxes))

暫無
暫無

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

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