[英]How to create a numpy array of a given type with numba
由於Numba 0.19,因此可以在nopython模式下顯式創建numpy數組。 如何創建給定類型的數組?
from numba import jit
import numpy as np
@jit(nopython=True)
def f():
a = np.zeros(5, dtype = np.int)
上面的代碼失敗,出現以下錯誤
TypingError: Failed at nopython (nopython frontend)
Undeclared Function(<built-in function zeros>)(int32, Function(<class 'int'>))
File "<ipython-input-4-3169be7a8201>", line 6
你應該使用numba
dtypes而不是numpy
import numba
import numpy as np
@numba.njit
def f():
a = np.zeros(5, dtype=numba.int32)
return a
In [8]: f()
Out[8]: array([0, 0, 0, 0, 0], dtype=int32)
在python 2.7中,似乎np.int確實有效。
至於來自dlenz的解決方法,我想指出使用np.int32也可以正常工作......如果你想在某些時候出於某種原因刪除numba.njit,那么代碼將無需修改即可工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.