基于与外部数组交集的numpy recarray索引 [英] numpy recarray indexing based on intersection with external array

查看:160
本文介绍了基于与外部数组交集的numpy recarray索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据其中一个重新排列字段和一个外部数组之间的公共值,在numpy.recarray中对记录进行子集化。例如,

I'm trying to subset the records in a numpy.recarray based on the common values between one of the recarrays fields and an external array. For example,

a = np.array([(10, 'Bob', 145.7), (20, 'Sue', 112.3), (10, 'Jim', 130.5)],
        dtype=[('id', 'i4'), ('name', 'S10'), ('weight', 'f8')])
a = a.view(np.recarray)

b = np.array([10,30])

我想采用a.id和b的交集来确定从重新排列中提取哪些记录,以便我回来:

I want to take the intersection of a.id and b to determine what records to pull from the recarray, so that I get back:

(10,'Bob',145.7)

(10,'Jim',130.5)

(10, 'Bob', 145.7)
(10, 'Jim', 130.5)

天真,我试过:

common = np.intersect1d(a.id, b)
subset = a[common]

但当然这不起作用,因为没有[10]。我也尝试通过在id字段和索引之间创建一个反向字典并从那里进行子集化来实现这一点,例如

but of course that doesn't work because there is no a[10]. I also tried to do this by creating a reverse dict between the id field and the index and subsetted from there, e.g.

id_x_index = {}
ids = a.id
indexes = np.arange(a.size)
for (id, index) in zip(ids, indexes):
    id_x_index[id] = index

subset_indexes = np.sort([id_x_index[x] for x in ids if x in b])
print a[subset_indexes]

但是如果a.id有重复项,我会覆盖id_x_index中的dict值,因为在这种情况下我得到了

but then I'm overriding dict values in id_x_index if a.id has duplicates, as in this case I get

(10,'Jim',130.5)

(10,'Jim',130.5)

(10, 'Jim', 130.5)
(10, 'Jim', 130.5)

我知道我忽略了一些简单的方法来获得适当的指数进入重组。感谢您的帮助。

I know I'm overlooking some simple way to get the appropriate indices into the recarray. Thanks for help.

推荐答案

在Numpy中执行此操作的最简洁方法是

The most concise way to do this in Numpy is

subset = a[np.in1d(a.id, b)]

这篇关于基于与外部数组交集的numpy recarray索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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