Number of unique elements per row in a NumPy array

后端 未结 4 589
执念已碎
执念已碎 2021-01-15 00:41

For example, for

a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])

I want to get

[2, 2, 3]

Is there a way

4条回答
  •  無奈伤痛
    2021-01-15 00:52

    A oneliner using sort:

    In [6]: np.count_nonzero(np.diff(np.sort(a)), axis=1)+1
    Out[6]: array([2, 2, 3])
    

提交回复
热议问题