Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotImplementedError: range_state_int64 cannot be represented as a Numpy dtype

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!

like image 525
Yun Wei Avatar asked Jul 05 '26 21:07

Yun Wei


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])
like image 189
cs95 Avatar answered Jul 07 '26 10:07

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!