One objective of Numba is having a seamless integration with NumPy. NumPy arrays provide an efficient storage method for homogeneous sets of data. NumPy dtypes provide type information useful when compiling, and the regular, structured storage of potentially large amounts of data in memory provides an ideal memory layout for code generation. Numba excels at generating code that executes on top of NumPy arrays.
NumPy support in Numba comes in many forms:
The following sections focus on the Numpy features supported in nopython mode, unless otherwise stated.
Numba supports the following Numpy scalar types:
The following scalar types and features are not supported:
The operations supported on scalar Numpy numbers are the same as on the
equivalent built-in types such as int
or float
. You can use
a type’s constructor to convert from a different type or width.
Structured scalars support attribute getting and setting, as well as member lookup using constant strings.
See also
Numpy scalars reference.
Numpy arrays of any of the scalar types above are supported, regardless of the shape or layout.
Arrays support normal iteration. Full basic indexing and slicing is supported. A subset of advanced indexing is also supported: only one advanced index is allowed, and it has to be a one-dimensional array (it can be combined with an arbitrary number of basic indices as well).
See also
Numpy indexing reference.
The following attributes of Numpy arrays are supported:
flags
object¶The object returned by the flags
attribute supports
the contiguous
, c_contiguous
and f_contiguous
attributes.
flat
object¶The object returned by the flat
attribute supports
iteration and indexing, but be careful: indexing is very slow on
non-C-contiguous arrays.
real
and imag
attributes¶Numpy supports these attributes regardless of the dtype but Numba chooses to
limit their support to avoid potential user error. For numeric dtypes,
Numba follows Numpy’s behavior. The real
attribute
returns a view of the real part of the complex array and it behaves as an identity
function for other numeric dtypes. The imag
attribute
returns a view of the imaginary part of the complex array and it returns a zero
array with the same shape and dtype for other numeric dtypes. For non-numeric
dtypes, including all structured/record dtypes, using these attributes will
result in a compile-time (TypingError) error. This behavior differs from
Numpy’s but it is chosen to avoid the potential confusion with field names that
overlap these attributes.
The following methods of Numpy arrays are supported in their basic form (without any optional arguments):
all()
any()
argmax()
argmin()
cumprod()
cumsum()
max()
mean()
min()
nonzero()
prod()
std()
take()
var()
The corresponding top-level Numpy functions (such as numpy.prod()
)
are similarly supported.
The following methods of Numpy arrays are supported:
argsort()
(without arguments)astype()
(only the 1-argument form)copy()
(without arguments)flatten()
(no order argument; ‘C’ order only)item()
(without arguments)itemset()
(only the 1-argument form)ravel()
(no order argument; ‘C’ order only)reshape()
(only the 1-argument form)sort()
(without arguments)sum()
(with or without the axis
argument)axis
argument is a compile-time constant, all valid values are supported.
An out-of-range value will result in a LoweringError
at compile-time.axis
argument is not a compile-time constant, only values from 0 to 3 are supported.
An out-of-range value will result in a runtime exception.transpose()
(without arguments, and without copying)view()
(only the 1-argument form)Warning
Sorting may be slightly slower than Numpy’s implementation.
Basic linear algebra is supported on 1-D and 2-D contiguous arrays of floating-point and complex numbers:
numpy.dot()
numpy.kron()
numpy.outer()
numpy.trace()
(only the first argument).numpy.vdot()
a @ b
where a
and b
are 1-D or 2-D arrays).numpy.linalg.cholesky()
numpy.linalg.cond()
(only non string values in p
).numpy.linalg.det()
numpy.linalg.eig()
(only running with data that does not cause a domain
change is supported e.g. real input -> real
output, complex input -> complex output).numpy.linalg.eigh()
(only the first argument).numpy.linalg.eigvals()
(only running with data that does not cause a
domain change is supported e.g. real input -> real output,
complex input -> complex output).numpy.linalg.eigvalsh()
(only the first argument).numpy.linalg.inv()
numpy.linalg.lstsq()
numpy.linalg.matrix_power()
numpy.linalg.matrix_rank()
numpy.linalg.norm()
(only the 2 first arguments and only non string
values in ord
).numpy.linalg.pinv()
numpy.linalg.qr()
(only the first argument).numpy.linalg.slogdet()
numpy.linalg.solve()
numpy.linalg.svd()
(only the 2 first arguments).Note
The implementation of these functions needs Scipy 0.16+ to be installed.
The following reduction functions are supported:
numpy.diff()
(only the 2 first arguments)numpy.median()
(only the first argument)numpy.nanmax()
(only the first argument)numpy.nanmean()
(only the first argument)numpy.nanmedian()
(only the first argument)numpy.nanmin()
(only the first argument)numpy.nanprod()
(only the first argument)numpy.nanstd()
(only the first argument)numpy.nansum()
(only the first argument)numpy.nanvar()
(only the first argument)The following top-level functions are supported:
numpy.arange()
numpy.argsort()
(no optional arguments)numpy.array()
(only the 2 first arguments)numpy.asfortranarray()
(only the first argument)numpy.atleast_1d()
numpy.atleast_2d()
numpy.atleast_3d()
numpy.bincount()
(only the 2 first arguments)numpy.column_stack()
numpy.concatenate()
numpy.copy()
(only the first argument)numpy.diag()
numpy.digitize()
numpy.dstack()
numpy.empty()
(only the 2 first arguments)numpy.empty_like()
(only the 2 first arguments)numpy.expand_dims()
numpy.eye()
numpy.flatten()
(no order argument; ‘C’ order only)numpy.frombuffer()
(only the 2 first arguments)numpy.full()
(only the 3 first arguments)numpy.full_like()
(only the 3 first arguments)numpy.histogram()
(only the 3 first arguments)numpy.hstack()
numpy.identity()
numpy.linspace()
(only the 3-argument form)numpy.ndenumerate
numpy.ndindex
numpy.nditer
(only the first argument)numpy.ones()
(only the 2 first arguments)numpy.ones_like()
(only the 2 first arguments)numpy.ravel()
(no order argument; ‘C’ order only)numpy.roots()
numpy.round_()
numpy.searchsorted()
(only the 2 first arguments)numpy.sinc()
numpy.sort()
(no optional arguments)numpy.stack()
numpy.take()
(only the 2 first arguments)numpy.vstack()
numpy.where()
numpy.zeros()
(only the 2 first arguments)numpy.zeros_like()
(only the 2 first arguments)The following constructors are supported, both with a numeric input (to construct a scalar) or a sequence (to construct an array):
numpy.bool_
numpy.complex64
numpy.complex128
numpy.float32
numpy.float64
numpy.int8
numpy.int16
numpy.int32
numpy.int64
numpy.intc
numpy.intp
numpy.uint8
numpy.uint16
numpy.uint32
numpy.uint64
numpy.uintc
numpy.uintp
The following machine parameter classes are supported, with all purely numerical attributes:
numpy.iinfo
numpy.finfo
(machar
attribute not supported)numpy.MachAr
(with no arguments to the constructor)Neither Python nor Numba has actual array literals, but you can construct
arbitrary arrays by calling numpy.array()
on a nested tuple:
a = numpy.array(((a, b, c), (d, e, f)))
(nested lists are not yet supported by Numba)
random
¶Numba supports top-level functions from the numpy.random module, but does not allow you to create individual RandomState instances. The same algorithms are used as for the standard random module (and therefore the same notes apply), but with an independent internal state: seeding or drawing numbers from one generator won’t affect the other.
The following functions are supported.
numpy.random.seed()
: with an integer argument onlynumpy.random.choice()
: the optional p argument (probabilities
array) is not supportednumpy.random.shuffle()
: the sequence argument must be a one-dimension
Numpy array or buffer-providing object (such as a bytearray
or array.array
)numpy.random.beta()
numpy.random.binomial()
numpy.random.chisquare()
numpy.random.exponential()
numpy.random.f()
numpy.random.gamma()
numpy.random.geometric()
numpy.random.gumbel()
numpy.random.hypergeometric()
numpy.random.laplace()
numpy.random.logistic()
numpy.random.lognormal()
numpy.random.logseries()
numpy.random.multinomial()
numpy.random.negative_binomial()
numpy.random.normal()
numpy.random.pareto()
numpy.random.poisson()
numpy.random.power()
numpy.random.rayleigh()
numpy.random.standard_cauchy()
numpy.random.standard_exponential()
numpy.random.standard_gamma()
numpy.random.standard_normal()
numpy.random.standard_t()
numpy.random.triangular()
numpy.random.uniform()
numpy.random.vonmises()
numpy.random.wald()
numpy.random.weibull()
numpy.random.zipf()
Note
Calling numpy.random.seed()
from non-Numba code (or from
object mode code) will seed the Numpy random generator, not the
Numba random generator.
Note
The generator is not thread-safe when releasing the GIL.
Also, under Unix, if creating a child process using os.fork()
or the
multiprocessing
module, the child’s random generator will inherit
the parent’s state and will therefore produce the same sequence of
numbers (except when using the “forkserver” start method under Python 3.4
and later).
stride_tricks
¶The following function from the numpy.lib.stride_tricks
module
is supported:
as_strided()
(the strides argument
is mandatory, the subok argument is not supported)One objective of Numba is having all the standard ufuncs in NumPy understood by Numba. When a supported ufunc is found when compiling a function, Numba maps the ufunc to equivalent native code. This allows the use of those ufuncs in Numba code that gets compiled in nopython mode.
Right now, only a selection of the standard ufuncs work in nopython mode. Following is a list of the different standard ufuncs that Numba is aware of, sorted in the same way as in the NumPy documentation.
UFUNC | MODE | |
---|---|---|
name | object mode | nopython mode |
add | Yes | Yes |
subtract | Yes | Yes |
multiply | Yes | Yes |
divide | Yes | Yes |
logaddexp | Yes | Yes |
logaddexp2 | Yes | Yes |
true_divide | Yes | Yes |
floor_divide | Yes | Yes |
negative | Yes | Yes |
power | Yes | Yes |
remainder | Yes | Yes |
mod | Yes | Yes |
fmod | Yes | Yes |
abs | Yes | Yes |
absolute | Yes | Yes |
fabs | Yes | Yes |
rint | Yes | Yes |
sign | Yes | Yes |
conj | Yes | Yes |
exp | Yes | Yes |
exp2 | Yes | Yes |
log | Yes | Yes |
log2 | Yes | Yes |
log10 | Yes | Yes |
expm1 | Yes | Yes |
log1p | Yes | Yes |
sqrt | Yes | Yes |
square | Yes | Yes |
reciprocal | Yes | Yes |
conjugate | Yes | Yes |
UFUNC | MODE | |
---|---|---|
name | object mode | nopython mode |
sin | Yes | Yes |
cos | Yes | Yes |
tan | Yes | Yes |
arcsin | Yes | Yes |
arccos | Yes | Yes |
arctan | Yes | Yes |
arctan2 | Yes | Yes |
hypot | Yes | Yes |
sinh | Yes | Yes |
cosh | Yes | Yes |
tanh | Yes | Yes |
arcsinh | Yes | Yes |
arccosh | Yes | Yes |
arctanh | Yes | Yes |
deg2rad | Yes | Yes |
rad2deg | Yes | Yes |
degrees | Yes | Yes |
radians | Yes | Yes |
UFUNC | MODE | |
---|---|---|
name | object mode | nopython mode |
bitwise_and | Yes | Yes |
bitwise_or | Yes | Yes |
bitwise_xor | Yes | Yes |
bitwise_not | Yes | Yes |
invert | Yes | Yes |
left_shift | Yes | Yes |
right_shift | Yes | Yes |
UFUNC | MODE | |
---|---|---|
name | object mode | nopython mode |
greater | Yes | Yes |
greater_equal | Yes | Yes |
less | Yes | Yes |
less_equal | Yes | Yes |
not_equal | Yes | Yes |
equal | Yes | Yes |
logical_and | Yes | Yes |
logical_or | Yes | Yes |
logical_xor | Yes | Yes |
logical_not | Yes | Yes |
maximum | Yes | Yes |
minimum | Yes | Yes |
fmax | Yes | Yes |
fmin | Yes | Yes |
UFUNC | MODE | |
---|---|---|
name | object mode | nopython mode |
isfinite | Yes | Yes |
isinf | Yes | Yes |
isnan | Yes | Yes |
signbit | Yes | Yes |
copysign | Yes | Yes |
nextafter | Yes | Yes |
modf | Yes | No |
ldexp | Yes (*) | Yes |
frexp | Yes | No |
floor | Yes | Yes |
ceil | Yes | Yes |
trunc | Yes | Yes |
spacing | Yes | Yes |
(*) not supported on windows 32 bit