TypeError when indexing numpy array using numba

左心房为你撑大大i 提交于 2019-12-11 04:35:02

问题


I need to sum up elements in a 1D numpy array (below: data) based on another array with information on class memberships (labels). I use numbain the code below to speed it up. However, If I dot not explicitly cast with int() in the line ret[int(find(labels, g))] += y, I reveice an error message:

TypeError: unsupported array index type ?int64

Is there a better workaround that explicit casting?

import numpy as np
from numba import jit

labels = np.array([45, 85, 99, 89, 45, 86, 348, 764])
n = int(1e3)
data = np.random.random(n)
groups = np.random.choice(a=labels, size=n, replace=True)

@jit(nopython=True)
def find(seq, value):
    for ct, x in enumerate(seq):
        if x == value:
            return ct

@jit(nopython=True)
def subsumNumba(data, groups, labels):
    ret = np.zeros(len(labels))
    for y, g in zip(data, groups):
        # not working without casting with int()
        ret[int(find(labels, g))] += y
    return ret

回答1:


The problem is that find can either return an int or None if it doesn't find anything, thus I think the ?int64 error. To avoid casting, you need to provide an int return value when find exits without finding the desired value and then handle it in the caller.



来源:https://stackoverflow.com/questions/39316939/typeerror-when-indexing-numpy-array-using-numba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!