Working and managing numpy arrays with numba
Why can't Numba jit compile a simple Numpy array operation?
Here is a minimal non-working example that reproduces Numba compilation failure
import numpy as np
from numba import jit
rows = 10
columns = 999999
A = np.empty((rows, columns))
b = np.linspace(0, 1, num=rows)
@jit(nopython=True)
def replicate(A, b):
for i in range(A.shape[1]):
A[:, i] = b
return A #optional
replicate(a, b)
With the following error:
TypingError: Failed at nopython (nopython frontend)
Cannot resolve setitem: array(float64, 1d, C, nonconst)[(slice3_type, int64)] = array(float64, 1d, C, nonconst)
File "<ipython-input-32-db24fbe2922f>", line 12
Am I doing something wrong?
As an aside, I need nopython mode because in my actual situation I need to perform array addition, scalar multiplication, and an array populated with other arrays. and I understand that in object mode I will not be able to do loop jitter and therefore I will not see any real performance improvement on runtime.
+3
source to share
1 answer
Numba does not support numpy slicing in mode nopython
. Try expanding the loops explicitly:
rows = 10
columns = 999999
a = np.empty((rows, columns))
b = np.linspace(0, 1, num=rows)
@jit(nopython=True)
def replicate(A, b):
for i in xrange(A.shape[0]):
for j in xrange(A.shape[1]):
A[i, j] = b[i]
return A #optional
replicate(a, b)
+2
source to share