nlcpy.expand_dims

nlcpy.expand_dims(a, axis)[ソース]

Expands the shape of an array.

Insert a new axis that will appear at the axis position in the expanded array shape.

Parameters
aarray_like

Input array.

axisint or tuple of ints

Position in the expanded axes where the new axis is placed.

Returns
resndarray

View of a with the number of dimensions increased by one.

参考

squeeze

Removes single-dimensional entries from the shape of an array.

reshape

Gives a new shape to an array without changing its data.

Examples

>>> import nlcpy as vp
>>> x = vp.array([1,2])
>>> x.shape
(2,)

The following is equivalent to x[vp.newaxis,:] or x[vp.newaxis]:

>>> y = vp.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = vp.expand_dims(x, axis=1)  # Equivalent to x[:,vp.newaxis]
>>> y
array([[1],
       [2]])
>>> y.shape
(2, 1)

axis may also be a tuple: >>> y = vp.expand_dims(x, axis=(0, 1)) >>> y array([[[1, 2]]])

>>> y = vp.expand_dims(x, axis=(2, 0))
>>> y
array([[[1],
        [2]]])

Note that some examples may use None instead of vp.newaxis. These are the same objects:

>>> vp.newaxis is None
True