I am trying to use numba to improve the speed of some code that I've written that is rather slow. The majority of the time spent is in a single function. First I tried using just
@jit
before my function definition, which improved timing a bit. Then, I tried using
@jit(nopython=True)
instead. From what I've read in the documentation, the numpy methods that I am using within the function should be supported (e.g. transpose). However, I am getting an error
Failed at nopython (nopython frontend)
Untyped global name 'transpose'
The behaviour of the nopython compilation mode is to essentially compile the decorated function so that it will run entirely without the involvement of the Python interpreter. This is the recommended and best-practice way to use the Numba jit decorator as it leads to the best performance.
Numba excels at generating code that executes on top of NumPy arrays. NumPy support in Numba comes in many forms: Numba understands calls to NumPy ufuncs and is able to generate equivalent native code for many of them. NumPy arrays are directly supported in Numba.
Large dataFor larger input data, Numba version of function is must faster than Numpy version, even taking into account of the compiling time. In fact, the ratio of the Numpy and Numba run time will depends on both datasize, and the number of loops, or more general the nature of the function (to be compiled).
In order to use transpose
, you need to call it (as the docs describe) in the form of a method of a numpy array. So the following works:
import numpy as np
import numba as nb
@nb.jit(nopython=True)
def func(x):
y = x.transpose() # or x.T
return y
x = np.random.normal(size=(4,4))
x_t = func(x)
But calling y = np.transpose(x)
in the function does not. I assume you're doing the latter. Note, I'm using Numba 0.25.0 for reference.
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