package np

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
val get_py : string -> Py.Object.t

Get an attribute of this module as a Py.Object.t. This is useful to pass a Python function to another function.

val arange : ?start:[ `F of float | `I of int ] -> ?step:[ `F of float | `I of int ] -> ?dtype:Dtype.t -> stop:[ `F of float | `I of int ] -> unit -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

arange(start, stop, step,, dtype=None)

Return evenly spaced values within a given interval.

Values are generated within the half-open interval ``start, stop)`` (in other words, the interval including `start` but excluding `stop`). For integer arguments the function is equivalent to the Python built-in `range` function, but returns an ndarray rather than a list. When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use `numpy.linspace` for these cases. Parameters ---------- start : number, optional Start of interval. The interval includes this value. The default start value is 0. stop : number End of interval. The interval does not include this value, except in some cases where `step` is not an integer and floating point round-off affects the length of `out`. step : number, optional Spacing between values. For any output `out`, this is the distance between two adjacent values, ``out[i+1] - out[i]``. The default step size is 1. If `step` is specified as a position argument, `start` must also be given. dtype : dtype The type of the output array. If `dtype` is not given, infer the data type from the other input arguments. Returns ------- arange : ndarray Array of evenly spaced values. For floating point arguments, the length of the result is ``ceil((stop - start)/step)``. Because of floating point overflow, this rule may result in the last element of `out` being greater than `stop`. See Also -------- numpy.linspace : Evenly spaced numbers with careful handling of endpoints. numpy.ogrid: Arrays of evenly spaced numbers in N-dimensions. numpy.mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions. Examples -------- >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])

val array_function_dispatch : ?module_:string -> ?verify:bool -> ?docs_from_dispatcher:bool -> dispatcher:Py.Object.t -> unit -> Py.Object.t

Decorator for adding dispatch with the __array_function__ protocol.

See NEP-18 for example usage.

Parameters ---------- dispatcher : callable Function that when called like ``dispatcher( *args, **kwargs)`` with arguments from the NumPy function call returns an iterable of array-like arguments to check for ``__array_function__``. module : str, optional __module__ attribute to set on new function, e.g., ``module='numpy'``. By default, module is copied from the decorated function. verify : bool, optional If True, verify the that the signature of the dispatcher and decorated function signatures match exactly: all required and optional arguments should appear in order with the same names, but the default values for all optional arguments should be ``None``. Only disable verification if the dispatcher's signature needs to deviate for some particular reason, e.g., because the function has a signature like ``func( *args, **kwargs)``. docs_from_dispatcher : bool, optional If True, copy docs from the dispatcher function onto the dispatched function, rather than from the implementation. This is useful for functions defined in C, which otherwise don't have docstrings.

Returns ------- Function suitable for decorating the implementation of a NumPy function.

val asarray : ?dtype:Dtype.t -> ?order:[ `C | `F ] -> [> `Ndarray ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

Convert the input to an array.

Parameters ---------- a : array_like Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. dtype : data-type, optional By default, the data-type is inferred from the input data. order : 'C', 'F', optional Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to 'C'.

Returns ------- out : ndarray Array interpretation of `a`. No copy is performed if the input is already an ndarray with matching dtype and order. If `a` is a subclass of ndarray, a base class ndarray is returned.

See Also -------- asanyarray : Similar function which passes through subclasses. ascontiguousarray : Convert input to a contiguous array. asfarray : Convert input to a floating point ndarray. asfortranarray : Convert input to an ndarray with column-major memory order. asarray_chkfinite : Similar function which checks input for NaNs and Infs. fromiter : Create an array from an iterator. fromfunction : Construct an array by executing a function on grid positions.

Examples -------- Convert a list into an array:

>>> a = 1, 2 >>> np.asarray(a) array(1, 2)

Existing arrays are not copied:

>>> a = np.array(1, 2) >>> np.asarray(a) is a True

If `dtype` is set, array is copied only if dtype does not match:

>>> a = np.array(1, 2, dtype=np.float32) >>> np.asarray(a, dtype=np.float32) is a True >>> np.asarray(a, dtype=np.float64) is a False

Contrary to `asanyarray`, ndarray subclasses are not passed through:

>>> issubclass(np.recarray, np.ndarray) True >>> a = np.array((1.0, 2), (3.0, 4), dtype='f4,i4').view(np.recarray) >>> np.asarray(a) is a False >>> np.asanyarray(a) is a True

val empty : ?dtype:Dtype.t -> ?order:[ `C | `F ] -> int list -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

empty(shape, dtype=float, order='C')

Return a new array of given shape and type, without initializing entries.

Parameters ---------- shape : int or tuple of int Shape of the empty array, e.g., ``(2, 3)`` or ``2``. dtype : data-type, optional Desired output data-type for the array, e.g, `numpy.int8`. Default is `numpy.float64`. order : 'C', 'F', optional, default: 'C' Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.

Returns ------- out : ndarray Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None.

See Also -------- empty_like : Return an empty array with shape and type of input. ones : Return a new array setting values to one. zeros : Return a new array setting values to zero. full : Return a new array of given shape filled with value.

Notes ----- `empty`, unlike `zeros`, does not set the array values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution.

Examples -------- >>> np.empty(2, 2) array([ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]) #uninitialized

>>> np.empty(2, 2, dtype=int) array([-1073741821, -1067949133], [ 496041986, 19249760]) #uninitialized

val fftfreq : ?d:[ `F of float | `I of int | `Bool of bool | `S of string ] -> n:int -> unit -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

Return the Discrete Fourier Transform sample frequencies.

The returned float array `f` contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.

Given a window length `n` and a sample spacing `d`::

f = 0, 1, ..., n/2-1, -n/2, ..., -1 / (d*n) if n is even f = 0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1 / (d*n) if n is odd

Parameters ---------- n : int Window length. d : scalar, optional Sample spacing (inverse of the sampling rate). Defaults to 1.

Returns ------- f : ndarray Array of length `n` containing the sample frequencies.

Examples -------- >>> signal = np.array(-2, 8, 6, 4, 1, 0, 3, 5, dtype=float) >>> fourier = np.fft.fft(signal) >>> n = signal.size >>> timestep = 0.1 >>> freq = np.fft.fftfreq(n, d=timestep) >>> freq array( 0. , 1.25, 2.5 , ..., -3.75, -2.5 , -1.25)

val fftshift : ?axes:[ `Shape_tuple of Py.Object.t | `I of int ] -> [> `Ndarray ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

Shift the zero-frequency component to the center of the spectrum.

This function swaps half-spaces for all axes listed (defaults to all). Note that ``y0`` is the Nyquist component only if ``len(x)`` is even.

Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to shift. Default is None, which shifts all axes.

Returns ------- y : ndarray The shifted array.

See Also -------- ifftshift : The inverse of `fftshift`.

Examples -------- >>> freqs = np.fft.fftfreq(10, 0.1) >>> freqs array( 0., 1., 2., ..., -3., -2., -1.) >>> np.fft.fftshift(freqs) array(-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.)

Shift the zero-frequency component only along the second axis:

>>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]) >>> np.fft.fftshift(freqs, axes=(1,)) array([ 2., 0., 1.], [-4., 3., 4.], [-1., -3., -2.])

val ifftshift : ?axes:[ `Shape_tuple of Py.Object.t | `I of int ] -> [> `Ndarray ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

The inverse of `fftshift`. Although identical for even-length `x`, the functions differ by one sample for odd-length `x`.

Parameters ---------- x : array_like Input array. axes : int or shape tuple, optional Axes over which to calculate. Defaults to None, which shifts all axes.

Returns ------- y : ndarray The shifted array.

See Also -------- fftshift : Shift zero-frequency component to the center of the spectrum.

Examples -------- >>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3) >>> freqs array([ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.]) >>> np.fft.ifftshift(np.fft.fftshift(freqs)) array([ 0., 1., 2.], [ 3., 4., -4.], [-3., -2., -1.])

val rfftfreq : ?d:[ `F of float | `I of int | `Bool of bool | `S of string ] -> n:int -> unit -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

Return the Discrete Fourier Transform sample frequencies (for usage with rfft, irfft).

The returned float array `f` contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.

Given a window length `n` and a sample spacing `d`::

f = 0, 1, ..., n/2-1, n/2 / (d*n) if n is even f = 0, 1, ..., (n-1)/2-1, (n-1)/2 / (d*n) if n is odd

Unlike `fftfreq` (but like `scipy.fftpack.rfftfreq`) the Nyquist frequency component is considered to be positive.

Parameters ---------- n : int Window length. d : scalar, optional Sample spacing (inverse of the sampling rate). Defaults to 1.

Returns ------- f : ndarray Array of length ``n//2 + 1`` containing the sample frequencies.

Examples -------- >>> signal = np.array(-2, 8, 6, 4, 1, 0, 3, 5, -3, 4, dtype=float) >>> fourier = np.fft.rfft(signal) >>> n = signal.size >>> sample_rate = 100 >>> freq = np.fft.fftfreq(n, d=1./sample_rate) >>> freq array( 0., 10., 20., ..., -30., -20., -10.) >>> freq = np.fft.rfftfreq(n, d=1./sample_rate) >>> freq array( 0., 10., 20., 30., 40., 50.)

val roll : ?axis:int list -> shift:int list -> [> `Ndarray ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Obj.t

Roll array elements along a given axis.

Elements that roll beyond the last position are re-introduced at the first.

Parameters ---------- a : array_like Input array. shift : int or tuple of ints The number of places by which elements are shifted. If a tuple, then `axis` must be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If an int while `axis` is a tuple of ints, then the same value is used for all given axes. axis : int or tuple of ints, optional Axis or axes along which elements are shifted. By default, the array is flattened before shifting, after which the original shape is restored.

Returns ------- res : ndarray Output array, with the same shape as `a`.

See Also -------- rollaxis : Roll the specified axis backwards, until it lies in a given position.

Notes ----- .. versionadded:: 1.12.0

Supports rolling over multiple dimensions simultaneously.

Examples -------- >>> x = np.arange(10) >>> np.roll(x, 2) array(8, 9, 0, 1, 2, 3, 4, 5, 6, 7) >>> np.roll(x, -2) array(2, 3, 4, 5, 6, 7, 8, 9, 0, 1)

>>> x2 = np.reshape(x, (2,5)) >>> x2 array([0, 1, 2, 3, 4], [5, 6, 7, 8, 9]) >>> np.roll(x2, 1) array([9, 0, 1, 2, 3], [4, 5, 6, 7, 8]) >>> np.roll(x2, -1) array([1, 2, 3, 4, 5], [6, 7, 8, 9, 0]) >>> np.roll(x2, 1, axis=0) array([5, 6, 7, 8, 9], [0, 1, 2, 3, 4]) >>> np.roll(x2, -1, axis=0) array([5, 6, 7, 8, 9], [0, 1, 2, 3, 4]) >>> np.roll(x2, 1, axis=1) array([4, 0, 1, 2, 3], [9, 5, 6, 7, 8]) >>> np.roll(x2, -1, axis=1) array([1, 2, 3, 4, 0], [6, 7, 8, 9, 5])

val set_module : Py.Object.t -> Py.Object.t

Decorator for overriding __module__ on a function or class.

Example usage::

@set_module('numpy') def example(): pass

assert example.__module__ == 'numpy'

OCaml

Innovation. Community. Security.