删除NumPy数组中具有重复项的行

删除NumPy数组中具有重复项的行

问题描述:

我有一个numpy值的(N,3)数组:

I have a (N,3) array of numpy values:

>>> vals = numpy.array([[1,2,3],[4,5,6],[7,8,7],[0,4,5],[2,2,1],[0,0,0],[5,4,3]])
>>> vals
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 7],
       [0, 4, 5],
       [2, 2, 1],
       [0, 0, 0],
       [5, 4, 3]])

我想从数组中删除具有重复值的行.例如,上述数组的结果应为:

I'd like to remove rows from the array that have a duplicate value. For example, the result for the above array should be:

>>> duplicates_removed
array([[1, 2, 3],
       [4, 5, 6],
       [0, 4, 5],
       [5, 4, 3]])

我不确定如何在不循环的情况下使用numpy有效地执行此操作(数组可能会很大).有人知道我该怎么做吗?

I'm not sure how to do this efficiently with numpy without looping (the array could be quite large). Anyone know how I could do this?

这是一个选择:

import numpy
vals = numpy.array([[1,2,3],[4,5,6],[7,8,7],[0,4,5],[2,2,1],[0,0,0],[5,4,3]])
a = (vals[:,0] == vals[:,1]) | (vals[:,1] == vals[:,2]) | (vals[:,0] == vals[:,2])
vals = numpy.delete(vals, numpy.where(a), axis=0)