二维等效于numpy.unique [英] 2D equivalent of numpy.unique

查看:108
本文介绍了二维等效于numpy.unique的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Nx2 numpy.ndarray中有一组离散坐标.

I have a set of discretized coordinates in a Nx2 numpy.ndarray.

我想获取每个唯一坐标集的计数和索引. numpy.unique 确实做到了这一点,但对于标量元素.

I would like to get the counts and indices of each of these unique coordinate sets. numpy.unique does exactly this, but for scalar elements.

是否有一些干净的方法可以使用numpy来做到这一点?

Is there some clean way to do this using numpy?

示例:

#input
coor = np.array([[10,10],[12,9],[10,5],[12,9]]) 
#output
unique_count = np.array([1,2,1])
unique_index = np.array([0,1,2]) #1 could also be 3

unique count,将给出每个唯一值的计数,即:[10,10]的1,[12,9]的2和[10,5]的1.然后,将找到与coor[unique_index]

unique count, would give the counts of each of the unique values, ie: 1 of [10,10], 2 of [12,9] and 1 of [10,5]. One would then find the values these correspond to with coor[unique_index]

推荐答案

您可以使用.count().index()列表的方法

You can use .count() and .index() list's methods

coor = np.array([[10, 10], [12, 9], [10, 5], [12, 9]])
coor_tuple = [tuple(x) for x in coor]
unique_coor = sorted(set(coor_tuple), key=lambda x: coor_tuple.index(x))
unique_count = [coor_tuple.count(x) for x in unique_coor]
unique_index = [coor_tuple.index(x) for x in unique_coor]

这篇关于二维等效于numpy.unique的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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