Numba autojit error on comparing numpy arrays

早过忘川 提交于 2019-12-05 20:00:50

问题


When I compare two numpy arrays inside my function I get an error saying only length-1 arrays can be converted to Python scalars:

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()

The error:

---------------------------------------------------------------------------
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:


This may be secondary to your issue, but the way you have autojit shown you will not get a speed increase. With numba you need to explicitly show the for loops like so:

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()

This works just fine.



来源:https://stackoverflow.com/questions/19644720/numba-autojit-error-on-comparing-numpy-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!