删除NumPy数组中具有重复项的行 [英] Removing rows with duplicates in a NumPy array
本文介绍了删除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)
这篇关于删除NumPy数组中具有重复项的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文