Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is numba throwing an error regarding numpy methods when (nopython=True)?

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'
like image 933
chia Avatar asked May 12 '16 21:05

chia


People also ask

What is Nopython in Numba?

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.

Does Numba work with NumPy?

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.

Is Numba better than NumPy?

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).


1 Answers

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.

like image 104
JoshAdel Avatar answered Nov 15 '22 04:11

JoshAdel