Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numba autojit error on comparing numpy arrays

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
like image 934
KartMan Avatar asked Oct 28 '13 20:10

KartMan


1 Answers

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.

like image 55
Daniel Avatar answered Nov 05 '22 05:11

Daniel