python - 比较 numpy 数组时出现 Numba autojit 错误

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

错误:

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

最佳答案

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

这很好用。

关于python - 比较 numpy 数组时出现 Numba autojit 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644720/

相关文章:

python - 使用python 3访问django POST请求中的嵌套对象

python numpy - 提高列余弦相似度的效率

Python 多处理抛出 Killed : 9

python - Crontab 停止 - Ubuntu 服务器的管道损坏

python - 如何在 Python 中将 CSV 文件的 UTC 转换为 CEST

python - 如何扩展模型用户?

python - 值错误 : Unable to determine number of fit parameters. "Problem in curve fitting"

python - Pandas 使用的存储内存比要求的要多得多

python - 将 Numba 与 scikit-learn 结合使用

python - Numba 签名协议(protocol)