Search code examples
pythonnumpymasked-array

Counting masked values from masked arrays


I have a numpy masked array called 'mask' and I was wondering how I would be able to count the amount of either True or False values in the mask?

The mask variable is created when looping through different datasets so it would be helpful if it would work for any random array size.


Solution

  • It's very simple:

    import numpy as np
    
    # create random T/F array
    Q = np.random.choice([True, False], (100, 100))
    
    n_true = Q.sum()