Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the number of nonzero elements in a numpy array?

Tags:

python

numpy

Is it possible to get the length of the nonzero elements in a numpy array without iterating over the array or masking the array. Speed is the main goal of calculating the length.

Essentially, something like len(array).where(array != 0).

If it changes the answer, each row will begin with zeros. The array is filled on the diagonal with zeros.

like image 220
Jzl5325 Avatar asked Sep 17 '12 00:09

Jzl5325


People also ask

How do you find non zero values in Python?

The nonzero() function in Python is used to return the indices (the index numbers or index positions) of the elements of an array that are non-zero.

How do you count the number of zero elements in an array?

To count all the zeros in an array, simply use the np. count_nonzero() function checking for zeros. It returns the count of elements inside the array satisfying the condition (in this case, if it's zero or not).

How do I count the number of items in a NumPy array?

Use bincount() to count True elements in a NumPy array In python, the numpy module provides a function bincount(arr), which returns a count of number of occurrences of each value in array of non-negative ints.


2 Answers

Assuming you mean total number of nonzero elements (and not total number of nonzero rows):

In [12]: a = np.random.randint(0, 3, size=(100,100))

In [13]: timeit len(a.nonzero()[0])
1000 loops, best of 3: 306 us per loop

In [14]: timeit (a != 0).sum()
10000 loops, best of 3: 46 us per loop

or even better:

In [22]: timeit np.count_nonzero(a)
10000 loops, best of 3: 39 us per loop

This last one, count_nonzero, seems to behave well when the array is small, too, whereas the sum trick not so much:

In [33]: a = np.random.randint(0, 3, size=(10,10))

In [34]: timeit len(a.nonzero()[0])
100000 loops, best of 3: 6.18 us per loop

In [35]: timeit (a != 0).sum()
100000 loops, best of 3: 13.5 us per loop

In [36]: timeit np.count_nonzero(a)
1000000 loops, best of 3: 686 ns per loop
like image 176
DSM Avatar answered Sep 20 '22 10:09

DSM


len(np.nonzero(array)[0]) ?

  • np.nonzero returns a tuple of indices, whose length is equal to the number of dimensions in the initial array
  • we get just the indices along the first dimension with [0]
  • compute its length with len
like image 22
Andrea Zonca Avatar answered Sep 18 '22 10:09

Andrea Zonca