Dominik Dominik - 1 year ago 198
Python Question

TypeError when indexing numpy array using numba

I need to sum up elements in a 1D

array (below:
) based on another array with information on class memberships (
). I use
in the code below to speed it up. However, If I dot not explicitly cast with
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)

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download