你如何在一个 numpy 数组中找到并保存重复的行? [英] how do you find and save duplicated rows in a numpy array?

查看:80
本文介绍了你如何在一个 numpy 数组中找到并保存重复的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,例如

Array = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[1,1,1],[2,2,2]]

我想要输出以下内容的东西:

And i would like something that would output the following:

Repeated = [[1,1,1],[2,2,2]]

保留重复行的数量也可以,例如

Preserving the number of repeated rows would work too, e.g.

Repeated = [[1,1,1],[1,1,1],[2,2,2],[2,2,2]]

我认为该解决方案可能包含 numpy.unique,但我无法使其正常工作,是否有本机 python/numpy 函数?

I thought the solution might include numpy.unique, but i can't get it to work, is there a native python / numpy function?

推荐答案

使用 np.unique 以及为我们提供唯一行的 return_counts=True以及每一行的相应计数,我们可以用 counts > 屏蔽掉这些行.1 从而得到我们想要的输出,就像这样 -

Using the new axis functionality of np.unique alongwith return_counts=True that gives us the unique rows and the corresponding counts for each of those rows, we can mask out the rows with counts > 1 and thus have our desired output, like so -

In [688]: a = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[1,1,1],[2,2,2]])

In [689]: unq, count = np.unique(a, axis=0, return_counts=True)

In [690]: unq[count>1]
Out[690]: 
array([[1, 1, 1],
       [2, 2, 2]])

这篇关于你如何在一个 numpy 数组中找到并保存重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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