1

I have two arrays (gt and pred) with values ranging from 0 to 4.

The shape of these two arrays is (1, 1, 93, 349, 219). My target is to create a mask to ignore and multiplying it into gt and pred to ignore the value in two arrays. However, I am facing an issue

ignore_value=4
if ignore_value is not None:
    mask[gt!=ignore_value]=1  # ignore value mask
    gt=mask*gt
    pred=mask*pred  # ignore value mask for pred


print "after removing ignore value: ", np.unique(gt),np.unique(pred)
output: after removing ignore value:  [0 1 2 3] [0 1 2 3 4]

why it is not removing the ignore value in pred?

6
  • How did you initialise mask? Commented Dec 23, 2018 at 8:56
  • @coldspeed mask=np.zeros(gt.shape, dtype=np.int32) Commented Dec 23, 2018 at 8:58
  • Other thing, is this code being called inside a function that you passed these arrays to? Commented Dec 23, 2018 at 8:58
  • @coldspeed yes, gt, pred, and ignore_value are input arguments passed to the function Commented Dec 23, 2018 at 9:00
  • 1
    Ah, okay. I get it. You initialised a mask based on gt, but pred may not necessarily have 4s in the same cells as gt does. Bottom line, you need two separate masks. Commented Dec 23, 2018 at 9:02

1 Answer 1

1

IMO, a better solution would be to use boolean indexing and explicitly set cells to 0.

gt[gt == ignore_value] = 0
pred[pred == ignore_value] = 0

In general, it is not guaranteed that gt and pred will have ignore_value in the same cells, so using a single mask for both of them is not appropriate.

However, the code above works and is more efficient because it is only operating on a small portion of the array, not all of it (as multiplying them would do). The is the output

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

2 Comments

thank you for your help, what if I write like this: gt=np.where(gt==ignore_value,0,gt) pred=np.where(pred==ignore_value,0,pred)
@S.EB It will work, but will generate a new array. It is not as efficient.

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.