The code is here:
import numba as nb
import numpy as np
@nb.njit
def func(size):
ary = np.array([np.arange(size),np.arange(size)+1,np.arange(size)-1]).T
X = np.array([ary[1:,0] - ary[:-1,2],
ary[1:,1] - ary[:-1,2],
ary[1:,0] - ary[1:,1]
])
return X
Z = func(10**9)
When I run the code, it gives me an error message and I don't really understand what's going on here. Do functions decorated by njit not support creating new arrays inside the functions? Error message is the following:
TypingError: Invalid use of Function(<built-in function array>) with argument(s) of type(s): (list(array(int64, 1d, C)))
* parameterized
In definition 0:
TypingError: array(int64, 1d, C) not allowed in a homogeneous sequence
raised from C:\Users\User\Anaconda3\lib\site-packages\numba\typing\npydecl.py:459
In definition 1:
TypingError: array(int64, 1d, C) not allowed in a homogeneous sequence
raised from C:\Users\User\Anaconda3\lib\site-packages\numba\typing\npydecl.py:459
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<built-in function array>)
[2] During: typing of call at C:/Users/User/Desktop/all python file/3.2.4/nb_datatype.py (65)
EDIT: I forgot to transpose the array before edit, it should be a 10^9 by 3 array.
Numba has quite a few decorators, we've seen @jit , but there's also: @njit - this is an alias for @jit(nopython=True) as it is so commonly used! @vectorize - produces NumPy ufunc s (with all the ufunc methods supported).
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. Access to Numpy arrays is very efficient, as indexing is lowered to direct memory accesses when possible.
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).
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.
Instantiating NumPy arrays via a list of NumPy arrays, or even a list of lists, is not supported by numba.njit
. Instead, use np.empty
and then assign values via NumPy indexing:
@nb.njit
def func(size):
row_count = 3
ary = np.empty((row_count, size))
ranger = np.arange(size)
ary[0] = ranger
ary[1] = ranger + 1
ary[2] = ranger - 1
X = np.empty((row_count, row_count - 1))
X[0] = ary[1:,0] - ary[:-1,2]
X[1] = ary[1:,1] - ary[:-1,2]
X[2] = ary[1:,0] - ary[1:,1]
return X
Z = func(10**2)
print(Z)
array([[-1., -4.],
[ 0., -3.],
[-1., -1.]])
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