Find unique rows in numpy.array

后端 未结 20 2850
独厮守ぢ
独厮守ぢ 2020-11-21 10:57

I need to find unique rows in a numpy.array.

For example:

>>> a # I have
array([[1, 1, 1, 0, 0, 0],
       [0, 1, 1, 1, 0, 0],
         


        
20条回答
  •  孤街浪徒
    2020-11-21 12:02

    None of these answers worked for me. I'm assuming as my unique rows contained strings and not numbers. However this answer from another thread did work:

    Source: https://stackoverflow.com/a/38461043/5402386

    You can use .count() and .index() list's methods

    coor = np.array([[10, 10], [12, 9], [10, 5], [12, 9]])
    coor_tuple = [tuple(x) for x in coor]
    unique_coor = sorted(set(coor_tuple), key=lambda x: coor_tuple.index(x))
    unique_count = [coor_tuple.count(x) for x in unique_coor]
    unique_index = [coor_tuple.index(x) for x in unique_coor]
    

提交回复
热议问题