2

I have numpy array:

np.random.seed(100)
mask = np.random.choice([True, False], size=(10,3))
print (mask)
[[ True  True False]
 [False False False]
 [ True  True  True] <- problem - all values True
 [ True  True False]
 [ True  True  True] <- problem - all values True
 [ True False  True]
 [ True False  True]
 [False  True  True]
 [ True False False]
 [False  True  True]]

Need in each row no all values True - so here can be only 0, 1 or 2 True because 3 'columns'.

Ugly solution is:

mask[:, -1] = False
print (mask)
[[ True  True False]
 [False False False]
 [ True  True False]
 [ True  True False]
 [ True  True False]
 [ True False False]
 [ True False False]
 [False  True False]
 [ True False False]
 [False  True False]]

What is better and more generic solution?

2
  • 2
    mask[mask.all(1),-1] = 0? That would create a different one than the one posted with the ugly solution though. Commented Feb 7, 2017 at 14:30
  • 1
    @Divakar your shorter code version beat my answer by a good few seconds Commented Feb 7, 2017 at 14:32

2 Answers 2

3

You could do:

In [109]:
mask[mask.all(axis=1),-1] = False
mask

Out[109]:
array([[ True,  True, False],
       [False, False, False],
       [ True,  True, False],
       [ True,  True, False],
       [ True,  True, False],
       [ True, False,  True],
       [ True, False,  True],
       [False,  True,  True],
       [ True, False, False],
       [False,  True,  True]], dtype=bool)

So just test row-wise using all and only set the 3rd col to False on this condition

Thanks to @Divakar, you could type less:

In [110]:
mask[mask.all(1),2] = 0
mask

Out[110]:
array([[ True,  True, False],
       [False, False, False],
       [ True,  True, False],
       [ True,  True, False],
       [ True,  True, False],
       [ True, False,  True],
       [ True, False,  True],
       [False,  True,  True],
       [ True, False, False],
       [False,  True,  True]], dtype=bool)

So here the position arg axis is being set and 0 is being cast to boolean False otherwise is same

Some explanation, first use all with axis=1 to test row-wise if all are True.

Then we use that mask to mask the rows in the square brackets, the second arg -1 selects the last column and finally we assign the new desired value

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

3 Comments

I would encourage you to add that shorter one as both are basically the same thing, if you would like :)
@Divakar sure will do
@dabadaba I'm using the original array as a mask, we test if all values are True row-wise when we pass axis=1, the -1 sets the last column to the desired value
2

Here's a fair one (all legal triplets equally likely):

N = 10
bits = np.random.randint(7, size=(N,))
mask = (bits[:, None] & 2**np.arange(3)).astype(bool)

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.