Numba with Dicts & np.zeros

Hi,

I’m new to numba, but excited by what I’ve read about it so far.

I’ve got a function like this:

import numba
from numba import jit, types
from numba.typed import Dict

@jit(nopython=True)
def metrics_from_matrix(confusion_matrix):
zero_matrix = np.zeros(confusion_matrix.shape, dtype=numba.int32)
metrics = Dict.empty(
key_type=types.unicode_type,
value_type=types.float64
)

I get a ‘non-precise type array(pyobject,1d,C)’ error on the “zero_matrix” line, and if I comment that out, on the “metrics = Dict” line as well. The full example is here: https://pastebin.com/yUd4EeXq

Any ideas on how to fix this?

thank you

this means that there’s an array that contains python objects, instead of a dtype like integer or float. Probably the labels variable which seems to be strings coming from a dataframe. Numba cannot handle arrays of object type.
you could try converting that array to a typed.List https://numba.pydata.org/numba-doc/dev/reference/pysupported.html#typed-list