比较numpy数组时出现Numba autojit错误 [英] Numba autojit error on comparing numpy arrays

查看:89
本文介绍了比较numpy数组时出现Numba autojit错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在函数中比较两个numpy数组时,我收到一条错误消息,指出只能将length-1数组转换为Python标量:

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

错误:

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

推荐答案

这可能是您的问题的第二要因,但是显示自动jijit的方式不会提高速度.使用numba,您需要像下面这样显式显示for循环:

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

这很好用.

这篇关于比较numpy数组时出现Numba autojit错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆