过滤numpy数组以仅保留给定值的一行 [英] Filter numpy array to retain only one row for a given value

查看:85
本文介绍了过滤numpy数组以仅保留给定值的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个较大的n x 2 numpy数组,其格式设置为(x,y)坐标.我想过滤此数组,以便:

I have a large n x 2 numpy array that is formatted as (x, y) coordinates. I would like to filter this array so as to:

  1. 识别具有重复x值的坐标对.
  2. 仅保留y值最高的重复项的坐标对.

例如,在以下数组中:

arr = [[1, 4]
       [1, 8]
       [2, 3]
       [4, 6]
       [4, 2]
       [5, 1]
       [5, 2]
       [5, 6]]

我希望结果是:

arr = [[1, 8]
       [2, 3]
       [4, 6]
       [5, 6]]

我已经研究过np.unique和np.where,但无法弄清楚如何利用它们来解决此问题.非常感谢!

Ive explored np.unique and np.where but cannot figure out how to leverage them to solve this problem. Thanks so much!

推荐答案

这是基于或者,如果您想带np.unique,我们可以用它来找到grp_idxnp.unique(b[:,0], return_index=1)[1].

Alternatively, if you want to bring np.unique, we can use it to find grp_idx with np.unique(b[:,0], return_index=1)[1].

样品运行-

In [453]: np.random.seed(0)

In [454]: arr = np.random.randint(0,5,(10,2))

In [455]: arr
Out[455]: 
array([[4, 0],
       [3, 3],
       [3, 1],
       [3, 2],
       [4, 0],
       [0, 4],
       [2, 1],
       [0, 1],
       [1, 0],
       [1, 4]])

In [456]: grouby_maxY(arr)
Out[456]: 
array([[0, 4],
       [1, 4],
       [2, 1],
       [3, 3],
       [4, 0]])

这篇关于过滤numpy数组以仅保留给定值的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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