count occurrences of arrays in multidimensional arrays in python

后端 未结 5 1706
醉酒成梦
醉酒成梦 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:59

    The numpy_indexed package (disclaimer: I am its author) contains efficient vectorized functionality for these kind of operations:

    import numpy_indexed as npi
    unique_rows, row_count = npi.count(a, axis=0)
    

    Note that this works for arrays of any dimensionality or datatype.

提交回复
热议问题