Python numba@njit将更新一个大目录
我尝试使用numba函数来搜索一个非常大(10e6)的dict,并使用(int,int)元组作为键Python numba@njit将更新一个大目录,python,jit,numba,Python,Jit,Numba,我尝试使用numba函数来搜索一个非常大(10e6)的dict,并使用(int,int)元组作为键 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 [
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
我想知道如何重写它,特别是dict_中的
if(I,j)和执行搜索的\u tuples\u键
部分。如果速度足够快,您也可以尝试:
from collections import Counter
c2 = Counter(dict_with_tuples_key)
c1 = Counter(tuple(x) for x in myarray)
new_dict = dict(c1 + c2)
表示函数是在nopython
模式下编译的。dict
、list
和tuple
是python对象,因此不受支持。不作为参数,也不在函数内部
如果你的DICT键不同,我会考虑使用一个2DUMPY数组,其中第一个轴代表DICT密钥元组的第一个索引,第二个轴代表第二个索引。然后您可以将其改写为:
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)
由于您已经使用元组为词典编制了索引,因此为数组编制索引的过渡问题将不会很好。我只是用您的解决方案来影响我的问题proposal@user3313834-请不要提出此类后续问题。如果我回答了原来的问题:好,那么就投票/接受/忽略我的回答。如果答案不能解决你的问题:等待另一个解决问题的答案。但在我看来,你有一个新问题,关于如何在numba中创建开放网格,这与最初的问题有些无关。那么请打开另一个问题,不要更新这个问题。好的,很抱歉,现在是一个专门的问题