Efficient way to count unique elements in array in numpy/scipy in Python

后端 未结 4 860
傲寒
傲寒 2021-02-02 15:16

I have a scipy array, e.g.

a = array([[0, 0, 1], [1, 1, 1], [1, 1, 1], [1, 0, 1]])

I want to count the number of occurrences of each unique ele

4条回答
  •  梦谈多话
    2021-02-02 15:30

    The numpy_indexed package (disclaimer: I am its author) provides a solution similar to the one posted by chuck; which is a nicely vectorized one. But with tests, a nice interface, and many more related useful functions:

    import numpy_indexed as npi
    npi.count(a)
    

提交回复
热议问题