2013-10-28 54 views
9

autojit錯誤,當我比較我的函數內的兩個numpy的陣列我得到一個錯誤說只有長度爲1的陣列可以轉換到Python標量:Numba上比較numpy的陣列

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 

回答

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

這一切正常。

+3

那麼,使用NumPy數組的主要動機之一就是利用它們的內置函數,而不必明確地重寫所有的實用程序。我剛剛舉了一個簡單的例子,其中Numba在NumPy數組上進行邏輯運算。但是,一般來說,我會遇到許多布爾/邏輯索引錯誤,如果您正在使用數組進行數字/科學編碼,這是一個非常有用的方法。 – KartMan