两个排序的部分重叠的numpy数组之间的索引映射 [英] Index mapping between two sorted partially overlapping numpy arrays

查看:111
本文介绍了两个排序的部分重叠的numpy数组之间的索引映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解决类似这样的问题,详情请参见找到两个之间的索引映射numpy数组,但是两个数组不一定包含相同的值集,尽管它们的值在每个数组中都是唯一的并已排序。

I want to solve something like the problem detailed at Find index mapping between two numpy arrays, but where the two arrays do not necessarily contain the same set of values, although their values are unique within each array, and are sorted.

例如如果我有两个数组:

E.g. if I have two arrays:

a = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
b = np.array([2.2, 3.0, 4.4, 6.0])

我想得到与 a 长度相同的数组,该索引将为匹配元素所在的 b 提供索引;如果为,则为-1没有匹配。即在这种情况下:

I want to get an array of the same length as a which gives the index into b where the matching element is, or -1 if there is no match. I.e. in this case:

map = np.array([-1, 0, -1, 2, -1])

是否有一种使用 np.searchsorted的简洁,快速的方法来实现此目的

推荐答案

使用搜索排序的索引检查匹配项,然后用无效的掩码掩盖无效的-说明符。对于匹配检查,请使用 idx 作为那些索引来执行 b [idx] == a 。因此-

Use the searchsorted indices to do a check on matches and then mask the invalid ones with the invalid-specifier. For the matching check, do b[idx]==a with idx as those indices. Hence -

invalid_specifier = -1
idx = np.searchsorted(b,a)
idx[idx==len(b)] = 0
out = np.where(b[idx]==a, idx, invalid_specifier)

这篇关于两个排序的部分重叠的numpy数组之间的索引映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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