对我来说很奇怪,当输入是数组列表时,vstack不能与Numba一起工作,它只在输入是数组元组时工作。示例代码:
@nb.jit(nopython=True)
def stack(items):
return np.vstack(items)
stack((np.array([1,2,3]), np.array([4,5,6])))
返回
array([[1, 2, 3],
[4, 5, 6]])
但
stack([np.array([1,2,3]), np.array([4,5,6])])
抛出一个错误
TypingError: No implementation of function Function(<function vstack at 0x0000027271963488>) found for signature:
>>>vstack(reflected list(array(int32, 1d, C)))
由于tuple不受支持,我努力寻找解决方法-我错过了什么吗?
2条答案
按热度按时间pkwftd7m1#
这是@hpaulj提到的一个解决方案:
x0fgdtte2#
在numba中,
vstack
、hstack
和concatenate
只支持tuple
作为输入,而不支持list
。他们说这是因为numba在编译Ref(https://github.com/numba/numba/issues/7476)时无法推断堆栈数组的维数。但我怀疑它实际上可以,因为你可以手动完成这一点,如下所示。
您可以通过以下间接方式堆叠
list
:或者,您可以定义一个自定义函数来执行堆栈工作: