Find unique rows in numpy.array

后端 未结 20 2857
独厮守ぢ
独厮守ぢ 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 11:56

    Lets get the entire numpy matrix as a list, then drop duplicates from this list, and finally return our unique list back into a numpy matrix:

    matrix_as_list=data.tolist() 
    matrix_as_list:
    [[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]]
    
    uniq_list=list()
    uniq_list.append(matrix_as_list[0])
    
    [uniq_list.append(item) for item in matrix_as_list if item not in uniq_list]
    
    unique_matrix=np.array(uniq_list)
    unique_matrix:
    array([[1, 1, 1, 0, 0, 0],
           [0, 1, 1, 1, 0, 0],
           [1, 1, 1, 1, 1, 0]])
    

提交回复
热议问题