Most efficient (and pythonic) way to count False values in 2D numpy arrays?

14,209

Solution 1

Use count_nonzero to count non-zero (e.g. not False) values:

>>> np.size(a) - np.count_nonzero(a)
2

Solution 2

The clearer is surely to ask exactly what is needed, but that doesn't mean it is the most efficient:

Using %%timeit in jupyter with python 2.7 on the proposed answers gives a clear winner:

    seq = [[True, True, False, True, False, False, False] * 10 for _ in range(100)]
    a = np.array(seq)

    np.size(a) - np.count_nonzero(a) 1000000 loops, best of 3: 1.34 µs per loop  - Antti Haapala
    (~a).sum()                        100000 loops, best of 3: 18.5 µs per loop  - Paul H
    np.size(a) - np.sum(a)             10000 loops, best of 3: 18.8 µs per loop  - OP
    len(a[a == False])                 10000 loops, best of 3: 52.4 µs per loop
    len(np.where(a==False))            10000 loops, best of 3: 77 µs per loop    - Forzaa
.

The clear winner is Antti Haapala, by an order of magnitude, with np.size(a) - np.count_nonzero(a)

len(np.where(a==False)) seems to be penalized by the nested structure of the array; the same benchmark on a 1 D array gives 10000 loops, best of 3: 27 µs per loop

Solution 3

This would do that:

len(np.where(a==False))

Maybe there are other ways that are faster or look better.

Solution 4

One alternative would be:

np.bitwise_not(a).sum()

Even shorter, but maybe less clear is:

(~a).sum()

Solution 5

Count Number of False Compare

number_of_false = np.size(out_putArray) - np.count_nonzero(out_putArray[0] >= out_putArray[1])

Count of Numbers of True Compare

number_of_true = np.count_nonzero(out_putArray[0] >= out_putArray[1])
Share:
14,209
G M
Author by

G M

About me I am an Italian analytical chemist specializing in the conservation of Cultural Heritage. I have a strong interest in science and IT for problem solving and divulgation. I enjoy learning new things and applying my knowledge to create new ones. Why am I here? I've learned Python and GIS on my own (but S.E. community really has helped me) so I'm trying to share my chemistry knowledge. I like helping people and I constantly try to improve my knowledge and skills ( so please correct my English mistakes!).

Updated on June 25, 2022

Comments

  • G M
    G M almost 2 years

    I am trying to count False value in an np.array like this:

    import numpy as np
    a = np.array([[True,True,True],[True,True,True],[True,False,False]])
    

    I usually use this method:

    number_of_false=np.size(a)-np.sum(a)
    

    Is there a better way?

  • G M
    G M about 8 years
    Yes but this is not the same solution that I use more or less?
  • Reblochon Masque
    Reblochon Masque about 8 years
    and from my little benchmark, by an order of magnitude at that. :)
  • BottleNick
    BottleNick about 4 years
    Btw: a.size is ~10x faster than np.size(a). An alternative to benchmark could be np.count_nonzero(~a), which will not be the most efficient, though.