Count the number of non zero values in a numpy array in Numba

SARose

Very simple. I am trying to count the number of non-zero values in an array in NumPy jit compiled with Numba (njit()). The following I've tried is not allowed by Numba.

  1. a[a != 0].size
  2. np.count_nonzero(a)
  3. len(a[a != 0])
  4. len(a) - len(a[a == 0])

I don't want to use for loops if there is still a faster, more pythonic and elegant way.

For that commenter that wanted to see a full code example...

import numpy as np
from numba import njit

@njit()
def n_nonzero(a):
    return a[a != 0].size
jdehesa

You may also consider, well, counting the nonzero values:

import numba as nb

@nb.njit()
def count_loop(a):
    s = 0
    for i in a:
        if i != 0:
            s += 1
    return s

I know it seems wrong, but bear with me:

import numpy as np
import numba as nb

@nb.njit()
def count_loop(a):
    s = 0
    for i in a:
        if i != 0:
            s += 1
    return s

@nb.njit()
def count_len_nonzero(a):
    return len(np.nonzero(a)[0])

@nb.njit()
def count_sum_neq_zero(a):
    return (a != 0).sum()

np.random.seed(100)
a = np.random.randint(0, 3, 1000000000, dtype=np.uint8)
c = np.count_nonzero(a)
assert count_len_nonzero(a) == c
assert count_sum_neq_zero(a) == c
assert count_loop(a) == c

%timeit count_len_nonzero(a)
# 5.94 s ± 141 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit count_sum_neq_zero(a)
# 848 ms ± 80.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit count_loop(a)
# 189 ms ± 4.41 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

It is in fact faster than np.count_nonzero, which can get quite slow for some reason:

%timeit np.count_nonzero(a)
# 4.36 s ± 69.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Flip non-zero values along each row of a lower triangular numpy array

分類Dev

Set specific values to zero in numpy array

分類Dev

Count number of rows before first non-zero

分類Dev

Numpy sum running length of non-zero values

分類Dev

Count non zero entry in row in R

分類Dev

Pandas datetime resample count non-zero

分類Dev

Unique count of array values

分類Dev

Count changing bits in numpy array

分類Dev

MongoDB count number of non-missing fields

分類Dev

Count non-zero pixels in area rotated rectangle

分類Dev

Count duplicate values in hash of an array

分類Dev

PHP - Count the values of an array not the keys?

分類Dev

Replace a string numpy array with a number

分類Dev

counting the number of non-zero numbers in a column of a df in pandas/python

分類Dev

Numpy: values in array wont change

分類Dev

Count number of unique values per row

分類Dev

pandas count number of occurrences of values in one column

分類Dev

Syntax to count the number of UNIQUE matching values

分類Dev

MySQL to PostgreSQL - Find in array, and count array values

分類Dev

Compiling all non-zero values in a row into a string column

分類Dev

Replace all values before last non-zero in a column with 0

分類Dev

How to change zero-terminated byte values with Numpy?

分類Dev

Changing the values in a number array in C

分類Dev

Return the number of unique number values in the array "arr"

分類Dev

How to Count number of counts in groupby clause count values

分類Dev

How do you change all values that are zero to another number?

分類Dev

numbaのnumpy.arrayの行を削除します

分類Dev

Count number of items in an array with a specific property value

分類Dev

counting unique values with it's associated count and total number of values

Related 関連記事

  1. 1

    Flip non-zero values along each row of a lower triangular numpy array

  2. 2

    Set specific values to zero in numpy array

  3. 3

    Count number of rows before first non-zero

  4. 4

    Numpy sum running length of non-zero values

  5. 5

    Count non zero entry in row in R

  6. 6

    Pandas datetime resample count non-zero

  7. 7

    Unique count of array values

  8. 8

    Count changing bits in numpy array

  9. 9

    MongoDB count number of non-missing fields

  10. 10

    Count non-zero pixels in area rotated rectangle

  11. 11

    Count duplicate values in hash of an array

  12. 12

    PHP - Count the values of an array not the keys?

  13. 13

    Replace a string numpy array with a number

  14. 14

    counting the number of non-zero numbers in a column of a df in pandas/python

  15. 15

    Numpy: values in array wont change

  16. 16

    Count number of unique values per row

  17. 17

    pandas count number of occurrences of values in one column

  18. 18

    Syntax to count the number of UNIQUE matching values

  19. 19

    MySQL to PostgreSQL - Find in array, and count array values

  20. 20

    Compiling all non-zero values in a row into a string column

  21. 21

    Replace all values before last non-zero in a column with 0

  22. 22

    How to change zero-terminated byte values with Numpy?

  23. 23

    Changing the values in a number array in C

  24. 24

    Return the number of unique number values in the array "arr"

  25. 25

    How to Count number of counts in groupby clause count values

  26. 26

    How do you change all values that are zero to another number?

  27. 27

    numbaのnumpy.arrayの行を削除します

  28. 28

    Count number of items in an array with a specific property value

  29. 29

    counting unique values with it's associated count and total number of values

ホットタグ

アーカイブ