numba @njit更新一个大字典

时间:2016-02-06 12:38:10

标签: python jit numba

我尝试将 numba 用于需要在(int,int)元组作为键的非常大(10e6)dict上进行搜索的函数。

import numpy as np
from numba import njit

myarray = np.array([[0, 0],  # 0, 1
                    [0, 1],
                    [1, 1],  # 1, 2
                    [1, 2],  # 1, 3
                    [2, 2],
                    [1, 3]]
) # a lot of this with shape~(10e6, 2)

dict_with_tuples_key = {(0, 1): 1,
                        (3, 7): 1} # ~10e6 keys 

简化版看起来像这样

# @njit
def update_dict(dict_with_tuples_key, myarray):
    for line in myarray:
        i, j = line
        if (i, j) in dict_with_tuples_key:
            dict_with_tuples_key[(i, j)] += 1
        else:
            dict_with_tuples_key[(i, j)] = 1
    return dict_with_tuples_key

new_dict = update_dict(dict_with_tuples_key, myarray)
print new_dict

new_dict = update_dict2(dict_with_tuples_key, myarray)
# print new_dict
# {(0, 1): 2,   # +1 already in dict_with_tuples_key
#  (0, 0): 1,   # diag
#  (1, 1): 1,   # diag
#  (2, 2): 1,   # diag
#  (1, 2): 1,   # new from myarray
#  (1, 3): 1,   # new from myarray
#  (3, 7): 1 }

看来@njit不接受dict作为函数arg?

我想知道如何重写这个,特别是进行搜索的if (i, j) in dict_with_tuples_key部分。

2 个答案:

答案 0 :(得分:5)

njit表示该函数是以nopython模式编译的。 dictlisttuple是python对象,因此不受支持。不作为参数而不是函数内部。

如果你的dict键都不同,我会考虑使用2D numpy数组,其中第一个轴代表dict-key-tuple的第一个索引,第二个轴代表第二个索引。然后你可以把它重写为:

from numba import njit
import numpy as np

@njit
def update_array(array, myarray):
    elements = myarray.shape[0]
    for i in range(elements):
        array[myarray[i][0]][myarray[i][1]] += 1 
    return array


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

# Calculate the size of the numpy array that replaces the dict:
lens = np.max(myarray, axis=0) # Maximum values
array = np.zeros((lens[0]+1, lens[1]+1)) # Create an empty array to hold all indexes in myarray
update_array(array, myarray)

由于您已经使用元组索引了字典,因此索引数组的转换问题不会很好。

答案 1 :(得分:0)

作为替代方案,你可以尝试这么快:

from collections import Counter


c2 = Counter(dict_with_tuples_key)
c1 = Counter(tuple(x) for x in myarray)
new_dict = dict(c1 + c2)