Skip to content
Advertisement

Zipping two np.array into array

Im trying to combine following two np.arrays into a single array:

prediction_score =
[0.99764085 0.26231623 0.07232302]

prediction_boxes =
[[282.25906   79.13187  420.98575  226.11221 ]
 [109.91688   94.8121   333.07764  225.87985 ]
 [340.3894    96.612015 601.4172   231.13196 ]]

The combination array must look like this [[i, pred, boxes],…]:

prediction_boxes =
[[1 0.99764085 282.25906   79.13187  420.98575  226.11221 ]
 [1 0.26231623 109.91688   94.8121   333.07764  225.87985 ]
 [1 0.07232302 340.3894    96.612015 601.4172   231.13196 ]]

I tried doing it this way, but it unfortunately didn’t work:

import numpy as np

i=1

for x in range(len(pred_scores)):
    np.insert(pred_bboxes[x], 0, pred_scores[x])
    np.insert(pred_bboxes[x], 0, i)
print(pred_bboxes)

Is there a way to do this? I tried other means but those tries were even worse.

Advertisement

Answer

Try hstack:

np.hstack(([[1]]*len(pred_boxes),     # classes
           pred_scores[...,None],     # scores
           pred_boxes)                # boxes
         )
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement