在 numpy.array 中查找唯一行 [英] Find unique rows in numpy.array

查看:37
本文介绍了在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆