在 numpy.array 中查找唯一行 [英] Find unique rows in numpy.array
问题描述
我需要在 numpy.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]])>>>new_a # 我想去数组([[1, 1, 1, 0, 0, 0],[0, 1, 1, 1, 0, 0],[1, 1, 1, 1, 1, 0]])我知道我可以创建一个集合并对数组进行循环,但我正在寻找一种高效的纯 numpy
解决方案.我相信有一种方法可以将数据类型设置为 void,然后我可以只使用 numpy.unique
,但我不知道如何使它工作.
从 NumPy 1.13 开始,您可以简单地选择轴来选择任何 N-dim 数组中的唯一值.要获得唯一的行,可以这样做:
unique_rows = np.unique(original_array,axis=0)
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],
[0, 1, 1, 1, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 0]])
>>> new_a # I want to get to
array([[1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 0]])
I know that i can create a set and loop over the array, but I am looking for an efficient pure numpy
solution. I believe that there is a way to set data type to void and then I could just use numpy.unique
, but I couldn't figure out how to make it work.
As of NumPy 1.13, one can simply choose the axis for selection of unique values in any N-dim array. To get unique rows, one can do:
unique_rows = np.unique(original_array, axis=0)
这篇关于在 numpy.array 中查找唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!