Numba autojit在比较numpy数组时出错

时间:2013-10-28 20:42:20

标签: python numpy numba

当我比较我的函数中的两个numpy数组时,我得到一个错误,说只有length-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

1 个答案:

答案 0 :(得分:3)

这可能是您的问题的次要问题,但是您显示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()

这很好用。