简体   繁体   中英

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

I need to add extra element to first position of nd array in numpy.

Here is the code:

tmp_boxes3d = [None,6]

tmp_scores = [None]

Now I need to add tmp_scores element into first position of tmp_boxes3d.

For better understanding

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

So I need to do for [None,6] shape.

Can anyone help me with this?

Try 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]])

Or, np.hstack

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM