zac*_*vac 4 python types numpy numba
我认为这是一个简单的问题,但我发现 numba 文档缺少如何将字符串类型与 numpy 数组和字典一起使用。我有一个函数,我想使用 numba,它接受邮政编码列表,然后是映射邮政编码 -> 值的字典。我知道我可以将 zips 转换为 int 数据类型,但我希望能够使用无法强制转换为 int 类型的其他值来执行此操作。
from numba.typed import Dict
from numba.core import types
@jit(nopython=True)
def zip_func(zip_array,zip_dict):
return zip_dict[zip_array[0]]
#This would be a longer array using real data
myzips = np.array(['12345'],dtype='<U5')
mydict = {"12345":5}
numba_dict = Dict.empty(key_type=types.unicode_type,value_type=types.int64)
for key,value in mydict.items():
numba_dict[key] = value
zip_func(myzips,numba_dict)
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误,如果我理解正确的话,当密钥键入为 types.unicode_type 时,基本上无法使用长度为 5 的固定长度字符串作为密钥。如何对齐这些类型?
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
<ipython-input-4-93b4a32d59d4> in <module>
12 for key,value in mydict.items():
13 numba_dict[key] = value
---> 14 zip_func(myzips,numba_dict)
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/dispatcher.py in _compile_for_args(self, *args, **kws)
399 e.patch_message(msg)
400
--> 401 error_rewrite(e, 'typing')
402 except errors.UnsupportedError as e:
403 # Something unsupported is present in the user code, add help info
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/dispatcher.py in error_rewrite(e, issue_type)
342 raise e
343 else:
--> 344 reraise(type(e), e, None)
345
346 argtypes = []
/sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/core/utils.py in reraise(tp, value, tb)
78 value = tp()
79 if value.__traceback__ is not tb:
---> 80 raise value.with_traceback(tb)
81 raise value
82
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.core.typeinfer.IntrinsicCallConstraint object at 0x7ff4562e2518>.
Failed in nopython mode pipeline (step: nopython mode backend)
Cannot cast [unichr x 5] to unicode_type: %".21" = load [5 x i32], [5 x i32]* %"key"
File "../../../python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/typed/dictobject.py", line 730:
def impl(d, key):
castedkey = _cast(key, keyty)
^
[1] During: lowering "$0.4 = call $0.1(key, $0.3, func=$0.1, args=[Var(key, dictobject.py:730), Var($0.3, dictobject.py:730)], kws=(), vararg=None)" at /sas/python/app/miniconda3/envs/py3lu/lib/python3.6/site-packages/numba/typed/dictobject.py (730)
[2] During: typing of intrinsic-call at <ipython-input-4-93b4a32d59d4> (7)
Enable logging at debug level for details.
File "<ipython-input-4-93b4a32d59d4>", line 7:
def zip_func(zip_array,zip_dict):
return zip_dict[zip_array[0]]
Run Code Online (Sandbox Code Playgroud)
您可以从 unichar dtype 创建自定义 Numba 类型,而不是用作numba.types.unicode_type字典。key_type
np_unichar = numpy.dtype('<U5')
UnicharType = numba.from_dtype(np_unichar)
Run Code Online (Sandbox Code Playgroud)
现在,您可以用来UnicharType告诉 Numba 您需要类型为 的值[unichr x 5]。
或者,我发现只需向 a 添加一个空字符串[unichr x N]就会强制进行类型转换。这意味着,zip_array[0] + ''将被处理为numba.types.unicode_type.