NotImplementedError: range_state_int64 cannot be represented as a Numpy dtype

Viewed 907

I am trying to write a function that initialises an array and shuffles it before returning. import numba as nb

@nb.jit(nopython=True, cache=True)
def test(x):
    ind = np.array(range(len(x)))
    np.random.shuffle(ind)
    return ind

Error message said that I used unsupported features or data types:

NotImplementedError: range_state_int64 cannot be represented as a Numpy dtype

Does numba supports numpy.random.shuffle() or not? How to revise it? Thank you!

1 Answers

This actually has nothing to do with random.shuffle, because numba supports the random module out of the box.

The issue here is that numba cannot support the range object (because it is a python object) when the nopython flag has been set. Substitute range with np.arange instead:

@nb.njit(cache=True)  # same as @nb.jit(nopython=True, ...)
def test(x):
    ind = np.arange(len(x))
    np.random.shuffle(ind)
    return ind

test([1, 2, 3])
# array([1, 0, 2])
Related