Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

np.newaxis with Numba nopython

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

like image 583
EntrustName Avatar asked Aug 04 '16 07:08

EntrustName


People also ask

Can I use Numba with NumPy?

Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops.

Does Numba make NumPy faster?

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.

What does Newaxis do in NumPy?

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.

Does Numba work with strings?

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.


1 Answers

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
like image 64
chrisb Avatar answered Sep 24 '22 03:09

chrisb