function atleast_nd¶
- deeptime.util.types.atleast_nd(ary, ndim, pos=0)¶
View input as array with at least ndim dimensions. New unit dimensions are inserted at the index given by pos if necessary.
- Parameters:
ary (array_like) – The input array. Non-array inputs are converted to arrays. Arrays that already have ndim or more dimensions are preserved.
ndim (scalar) – The minimum number of dimensions required.
pos (int, optional) – The index to insert the new dimensions. May range from
-ary.ndim - 1
to+ary.ndim
(inclusive). Non-negative indices indicate locations before the corresponding axis:pos=0
means to insert at the very beginning. Negative indices indicate locations after the corresponding axis:pos=-1
means to insert at the very end. 0 and -1 are always guaranteed to work. Any other number will depend on the dimensions of the existing array. Default is 0.
- Returns:
res – An array with
res.ndim >= ndim
. A view is returned for array inputs. Dimensions are prepended if pos is 0, so for example, a 1-D array of shape(N,)
withndim=4
becomes a view of shape(1, 1, 1, N)
. Dimensions are appended if pos is -1, so for example a 2-D array of shape(M, N)
becomes a view of shape(M, N, 1, 1)
whenndim=4
.- Return type:
ndarray
See also
np.atleast_1d
,np.atleast_2d
,np.atleast_3d
Notes
Taken from https://github.com/numpy/numpy/pull/7804.
This function does not follow the convention of the other atleast_*d functions in numpy in that it only accepts a single array argument. To process multiple arrays, use a comprehension or loop around the function call. See examples below. Setting
pos=0
is equivalent to how the array would be interpreted by numpy’s broadcasting rules. There is no need to call this function for simple broadcasting. This is also roughly (but not exactly) equivalent tonp.array(ary, copy=False, subok=True, ndmin=ndim)
. It is easy to create functions for specific dimensions similar to the other atleast_*d functions using Python’s functools.partial function. An example is shown below.