Python numba fingerprint error

I am trying numba to optimize some code. I looked at the source examples in section 1.3.1 in the 0.26.0 user guide ( http://numba.pydata.org/numba-doc/0.26.0/user/jit.html ) and get the expected results, so I don’t think what the problem is installation.

Here is my code:

import numba import numpy import random a = 8 b = 4 def my_function(a, b): all_values = numpy.fromiter(range(a), dtype = int) my_array = [] for n in (range(a)): some_values = (all_values[all_values != n]).tolist() c = random.sample(some_values, b) my_array.append(sorted([n] + c)) return my_array print(my_function(a, b)) my_function_numba = numba.jit()(my_function) print(my_function_numba(a, b)) 

Which, after printing the expected results of the my_function call, returns the following error message:

 ValueError Traceback (most recent call last) <ipython-input-8-b5d8983a58f6> in <module>() 19 my_function_numba = numba.jit()(my_function) 20 ---> 21 print(my_function_numba(a, b)) ValueError: cannot compute fingerprint of empty list 

Fingerprint of an empty list?

+6
source share
1 answer

I am not sure about this error, but, in general, fast numba requires a specific subset of numpy / python (see here and here for more). So I can rewrite it like this.

 @numba.jit(nopython=True) def fast_my_function(a, b): all_values = np.arange(a) my_array = np.empty((a, b + 1), dtype=np.int32) for n in range(a): some = all_values[all_values != n] c = np.empty(b + 1, dtype=np.int32) c[1:] = np.random.choice(some, b) c[0] = n c.sort() my_array[n, :] = c return my_array 

Key notes:

  • no lists, I pre-select everything.
  • lack of use of generators (in both python 2 and 3 for n in range(a) will be converted to a fast native loop)
  • adding nopython=True to the decorator makes it so numba will complain if I use something that cannot be effectively JITed.
+3
source

All Articles