Issue
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!
Solution
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?
Answered By - Ahsan Tarique
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.