Find unique rows in numpy.array

后端 未结 20 2847
独厮守ぢ
独厮守ぢ 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]
    
    0 讨论(0)
  • 2020-11-21 12:03
    import numpy as np
    original = np.array([[1, 1, 1, 0, 0, 0],
                         [0, 1, 1, 1, 0, 0],
                         [0, 1, 1, 1, 0, 0],
                         [1, 1, 1, 0, 0, 0],
                         [1, 1, 1, 1, 1, 0]])
    # create a view that the subarray as tuple and return unique indeies.
    _, unique_index = np.unique(original.view(original.dtype.descr * original.shape[1]),
                                return_index=True)
    # get unique set
    print(original[unique_index])
    
    0 讨论(0)
提交回复
热议问题