chia
chia

Reputation: 155

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'

Upvotes: 7

Views: 5192

Answers (1)

JoshAdel
JoshAdel

Reputation: 68682

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.

Upvotes: 8

Related Questions