1

I tried to execute this code, but when I call it for the first time to compile numba is angry on a line gen_code = np.array([]) :

char_array = np.array(['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
                       's','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J',
                       'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1',
                       '2','3','4','5','6','7','8','9'])

# creating a random 40-lenghted code from characters in char_array and returning as python string
@nb.njit(parallel=True)
def generateCode():
    gen_code = np.array([])
    for _ in nb.prange(40):
        gen_code = np.append(gen_code, char_array[random.randint(0, 61)])
    return ''.join(gen_code)

Full exception:

Type of variable 'gen_code' cannot be determined, operation: call $4load_method.1($6build_list.2, func=$4load_method.1, args=[Var($6build_list.2, my_new_project.py:21)], kws=(), vararg=None, varkwarg=None, target=None), location: d:\python_docs\Projects\my_new_project.py (21)

where 21 is that line I mentioned above. Why numba cannot initialize common numpy array? Thanks for your help!

Spin
  • 13
  • 4
  • 1
    Even in python code, `np.char.array` is discouraged. Make regular array with a char dtype, and use `np.char` functions to do string manipulations. In effect your `np.char.array([])` is not a common numpy array. And those aren't "symbol arrays". – hpaulj Oct 06 '22 at 06:11
  • @Spin please don't edit Answers into your Question when it fundamentally changes them to be solved, or it's much harder to understand and discover later by the community! – ti7 Oct 06 '22 at 12:28
  • fine fine, just tried to correct my mistakes in code, like using numba and nb in the same time – Spin Oct 06 '22 at 12:31

1 Answers1

2

Did you try creating an np array with a pre-defined shape and then iterating?

Something similar to this:

@nb.njit(parallel=True)
def generateCode():
    gen_code = np.empty(shape=(R), dtype = 'char') #replace R with correct value
    for i in numba.prange(R):
        gen_code[i] =gen_code, char_array[random.randint(0, 61)]
    return ''.join(gen_code)

See this post which explains why np.append() is not the right way to think about it: How do I create an empty array and then append to it in NumPy?

Ahsan Tarique
  • 581
  • 1
  • 11
  • 22