Is there a way to use np.newaxis
with Numba nopython
? In order to apply broadcasting function without fallbacking on python ?
for example
@jit(nopython=True)
def toto():
a = np.random.randn(20, 10)
b = np.random.randn(20)
c = np.random.randn(10)
d = a - b[:, np.newaxis] * c[np.newaxis, :]
return d
Thanks
Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops.
Numba can speed things up Of course, it turns out that NumPy has a function that will do this already, numpy. maximum. accumulate . Using that, running only takes 0.03 seconds.
Simply put, numpy. newaxis is used to increase the dimension of the existing array by one more dimension, when used once. Thus, 1D array will become 2D array.
Numba supports (Unicode) strings in Python 3. Strings can be passed into nopython mode as arguments, as well as constructed and returned from nopython mode.
You can accomplish this using reshape, it looks like the [:, None]
indexing isn't currently supported. Note that this probably won't be much faster than doing it python, since it was already vectorized.
@jit(nopython=True)
def toto():
a = np.random.randn(20, 10)
b = np.random.randn(20)
c = np.random.randn(10)
d = a - b.reshape((-1, 1)) * c.reshape((1,-1))
return d
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With