Expand the shape of an array.
Insert a new axis, corresponding to a given position in the array shape.
Parameters: | a : array_like
axis : int
|
---|---|
Returns: | res : ndarray
|
See also
doc.indexing, atleast_1d, atleast_2d, atleast_3d
Examples
>>> x = np.array([1,2])
>>> x.shape
(2,)
The following is equivalent to x[np.newaxis,:] or x[np.newaxis]:
>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = np.expand_dims(x, axis=1) # Equivalent to x[:,newaxis]
>>> y
array([[1],
[2]])
>>> y.shape
(2, 1)
Note that some examples may use None instead of np.newaxis. These are the same objects:
>>> np.newaxis is None
True