当我在我的函数中比较两个 numpy 数组时,我收到一条错误消息,指出只有长度为 1 的数组可以转换为 Python 标量:
from numpy.random import rand
from numba import autojit
@autojit
def myFun():
a = rand(10,1)
b = rand(10,1)
idx = a > b
return idx
myFun()
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-f7b68c0872a3> in <module>()
----> 1 myFun()
/Users/Guest/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numba/numbawrapper.so in numba.numbawrapper._NumbaSpecializingWrapper.__call__ (numba/numbawrapper.c:3764)()
TypeError: only length-1 arrays can be converted to Python scalars
最佳答案
这可能不是您的问题的次要问题,但是您显示 autojit 的方式不会提高速度。使用 numba,您需要像这样显式显示 for
循环:
from numpy.random import rand
from numba import autojit
@autojit
def myFun():
a = rand(10,1)
b = rand(10,1)
idx = np.zeros((10,1),dtype=bool)
for x in range(10):
idx[x,0] = a[x,0] > b[x,0]
return idx
myFun()
这很好用。
关于python - 比较 numpy 数组时出现 Numba autojit 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644720/