Search code examples
pythonnumpyboolean-operations

At least one True value per column in numpy boolean array


Suppose I have a very big 2D boolean array (for the sake of the example, let's take dimensions 4 lines x 3 columns):

toto = np.array([[True, True, False],
                [False, True, False],
                [True, False, False],
                [False, True, False]])

I want to transform totoso that it contains at least one True value per column , by leaving other columns untouched.

EDIT : The rule is just this : If a column is all False, I want to introduce a True in a random line.

So in this example, one of the False in the 3rd column should become True.

How would you do that efficiently?

Thank you in advance


Solution

  • You can do it like this:

    col_mask = ~np.any(toto, axis=0)
    row_idx = np.random.randint(toto.shape[0], size=np.sum(col_mask))
    toto[row_idx, col_mask]=True
    

    col_mask is array([False, False, True]) of changeable columns. row_idx is array that consists of changeable indexes of rows.