Kar*_*Man 9 python numpy numba
当我比较我的函数中的两个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()
Run Code Online (Sandbox Code Playgroud)
错误:
---------------------------------------------------------------------------
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
Run Code Online (Sandbox Code Playgroud)
这可能对您的问题来说是次要的,但您的自动抖动显示的方式不会提高速度。使用 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()
Run Code Online (Sandbox Code Playgroud)
这很好用。
| 归档时间: |
|
| 查看次数: |
932 次 |
| 最近记录: |