0

I have a numpy array of IoU values with 300 rows and 4 columns. A row is selected if every element in that row is less than 0.5. I wrote a code which try to do this but it returns rows in which every element is zero.

import numpy as np
iou = np.random.rand(300,4)

negative_boxes = []
for x in range(len(iou)):
    if iou[x,:].any() < 0.5:
       negative_boxes.append(iou[x])

How to select rows in which every element is less than 0.5?

2 Answers 2

2

Instead of a for loop, you can use numpy masks, that are more efficient.

With your problem:

import numpy as np
iou = np.random.rand(300,4)
indices = np.where((iou < 0.5).all(axis=1))
negative_boxes = iou[indices]

Then indices contains all the indices of the rows where all values are smaller than 0.5 and negative_boxes contains the array with only the small values you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

1

a.any() returns True if any of the elements of a evaluate to True, False otherwise.

if iou[x,:].any() < 0.5 implicitly converts the boolean value returned by iou[x,:].any() to 0 and 1 (in fact, bool is a subclass of int). Thus iou[x,:].any() < 0.5 is True if and only if iou[x,:].any() is False, i.e., every element of iou[x,:] is 0.

To check if all elements of an array a are less than 0.5, use np.all:

import numpy as np
iou = np.random.rand(300,4)

negative_boxes = []
for x in range(len(iou)):
    if np.all(iou[x, :] < 0.5):
       negative_boxes.append(iou[x])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.