Counting how many times a row occurs in a matrix (numpy)

后端 未结 1 987
时光取名叫无心
时光取名叫无心 2020-12-06 13:41

Is there a better way to count how many times a given row appears in a numpy 2D array than

def get_count(array_2d, row):
    count = 0
    # iterate over row         


        
相关标签:
1条回答
  • 2020-12-06 13:49

    One simple way would be with broadcasting -

    (array_2d == row).all(-1).sum()
    

    Considering memory efficiency, here's one approach considering each row from array_2d as an indexing tuple on an n-dimensional grid and assuming positive numbers in the inputs -

    dims = np.maximum(array_2d.max(0),row) + 1
    array_1d = np.ravel_multi_index(array_2d.T,dims)
    row_scalar = np.ravel_multi_index(row,dims)
    count = (array_1d==row_scalar).sum()
    

    Here's a post discussing the various aspects related to it.

    Note: Using np.count_nonzero could be much faster to count booleans instead of summation with .sum(). So, do consider using it for both the above mentioned aproaches.

    Here's a quick runtime test -

    In [74]: arr = np.random.rand(10000)>0.5
    
    In [75]: %timeit arr.sum()
    10000 loops, best of 3: 29.6 µs per loop
    
    In [76]: %timeit np.count_nonzero(arr)
    1000000 loops, best of 3: 1.21 µs per loop
    
    0 讨论(0)
提交回复
热议问题