count occurrences of arrays in multidimensional arrays in python

后端 未结 5 1704
醉酒成梦
醉酒成梦 2020-12-20 18:11

I have the following type of arrays:

a = array([[1,1,1],
           [1,1,1],
           [1,1,1],
           [2,2,2],
           [2,2,2],
           [2,2,2],
         


        
5条回答
  •  無奈伤痛
    2020-12-20 18:51

    You could convert those rows to a 1D array using the elements as two-dimensional indices with np.ravel_multi_index. Then, use np.unique to give us the positions of the start of each unique row and also has an optional argument return_counts to give us the counts. Thus, the implementation would look something like this -

    def unique_rows_counts(a):
    
        # Calculate linear indices using rows from a
        lidx = np.ravel_multi_index(a.T,a.max(0)+1 )
    
        # Get the unique indices and their counts
        _, unq_idx, counts = np.unique(lidx, return_index = True, return_counts=True)
    
        # return the unique groups from a and their respective counts
        return a[unq_idx], counts
    

    Sample run -

    In [64]: a
    Out[64]: 
    array([[1, 1, 1],
           [1, 1, 1],
           [1, 1, 1],
           [2, 2, 2],
           [2, 2, 2],
           [2, 2, 2],
           [3, 3, 0],
           [3, 3, 0],
           [3, 3, 0]])
    
    In [65]: unqrows, counts = unique_rows_counts(a)
    
    In [66]: unqrows
    Out[66]: 
    array([[1, 1, 1],
           [2, 2, 2],
           [3, 3, 0]])
    In [67]: counts
    Out[67]: array([3, 3, 3])
    

    Benchmarking

    Assuming you are okay with either numpy arrays or collections as outputs, one can benchmark the solutions provided thus far, like so -

    Function definitions:

    import numpy as np
    from collections import Counter
    
    def unique_rows_counts(a):
        lidx = np.ravel_multi_index(a.T,a.max(0)+1 )
        _, unq_idx, counts = np.unique(lidx, return_index = True, return_counts=True)
        return a[unq_idx], counts
    
    def map_Counter(a):
        return Counter(map(tuple, a))    
    
    def forloop_Counter(a):      
        c = Counter()
        for x in a:
            c[tuple(x)] += 1
        return c
    

    Timings:

    In [53]: a = np.random.randint(0,4,(10000,5))
    
    In [54]: %timeit map_Counter(a)
    10 loops, best of 3: 31.7 ms per loop
    
    In [55]: %timeit forloop_Counter(a)
    10 loops, best of 3: 45.4 ms per loop
    
    In [56]: %timeit unique_rows_counts(a)
    1000 loops, best of 3: 1.72 ms per loop
    

提交回复
热议问题