numpy:使用字典作为地图有效地替换2D数组中的值 [英] Numpy: Replacing values in a 2D array efficiently using a dictionary as a map

查看:105
本文介绍了numpy:使用字典作为地图有效地替换2D数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维的Numpy整数数组,如下所示:

I have a 2D Numpy array of integers like so:

a = np.array([[  3,   0,   2,  -1],
              [  1, 255,   1,   2],
              [  0,   3,   2,   2]])

,我有一个包含整数键和值的字典,我想用它替换 a 的值新价值。字典可能看起来像这样:

and I have a dictionary with integer keys and values that I would like to use to replace the values of a with new values. The dict might look like this:

d = {0: 1, 1: 2, 2: 3, 3: 4, -1: 0, 255: 0}

我要替换 a 匹配 d 中的键和 d 中的对应值。换句话说, d 定义了 a 中旧值(当前值)和新值(期望值)之间的映射。上面的玩具示例的结果如下:

I want to replace the values of a that match a key in d with the corresponding value in d. In other words, d defines a map between old (current) and new (desired) values in a. The outcome for the toy example above would be this:

a_new = np.array([[  4,   1,   3,   0],
                  [  2,   0,   2,   3],
                  [  1,   4,   3,   3]])

什么是实现此目标的有效方法?

What would be an efficient way to implement this?

这是一个玩具示例,但实际上数组会很大,它的形状例如(1024,2048),字典将包含数十个元素(在我的情况下为34个),并且键是整数,但它们不一定全部

This is a toy example, but in practice the array will be large, its shape will be e.g. (1024, 2048), and the dictionary will have on the order of dozens of elements (34 in my case), and while the keys are integers, they are not necessarily all consecutive and they can be negative (like in the example above).

我需要在成千上万个这样的数组上执行此替换,因此它需要快速。但是,字典是事先已知的,并且保持不变,因此,渐近地,用于修改字典或将其转换为更合适的数据结构的任何时间都无关紧要。

I need to perform this replacement on hundreds of thousands of such arrays, so it needs to be fast. However, the dictionary is known in advance and remains constant, so asymptotically, any time used to modify the dictionary or transform it into a more appropriate data structure doesn't matter.

我目前正在两个嵌套的中为循环遍历数组条目(在 a 的行和列中) ,但必须有更好的方法。

I'm currently looping over the array entries in two nested for loops (over the rows and columns of a), but there has got to be a better way.

如果地图不包含负值(例如,示例中为-1),则只需创建一个从字典中列出或排列一个数组,其中键是数组索引,然后将其用于有效的Numpy花式索引例程。

If the map didn't contain negative values (e.g. -1 like in the example), I would just create a list or an array from the dictionary once where the keys are the array indices and then use that for an efficient Numpy fancy indexing routine. But since there are negative values, too, this won't work.

推荐答案

如果您有一本小词典,这是一种方法/ min和max值,这样可能会更有效,您可以通过添加数组min来解决负索引问题:

Here's one way, provided you have a small dictionary/min and max values, this may be more efficient, you work around the negative index by adding the array min:

In [11]: indexer = np.array([d.get(i, -1) for i in range(a.min(), a.max() + 1)])

In [12]: indexer[(a - a.min())]
Out[12]:
array([[4, 1, 3, 0],
       [2, 0, 2, 3],
       [1, 4, 3, 3]])

注意:这会将for循环移到查找表,但是如果它比实际数组小得多,则可能会快得多。

这篇关于numpy:使用字典作为地图有效地替换2D数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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