1. numpy.
moveaxis
(
a,
source,
destination
)
[source]
Move axes of an array to new positions.
Other axes remain in their original order.
New in version 1.11.0.
Parameters: | a : np.ndarray
source : int or sequence of int
destination : int or sequence of int
|
---|---|
Returns: | result : np.ndarray
|
See also
transpose
swapaxes
Examples
>>> x = np.zeros((3, 4, 5))
>>> np.moveaxis(x, 0, -1).shape
(4, 5, 3)
>>> np.moveaxis(x, -1, 0).shape
(5, 3, 4)
These all achieve the same result:
>>> np.transpose(x).shape (5, 4, 3) >>> np.swapaxes(x, 0, -1).shape (5, 4, 3) >>> np.moveaxis(x, [0, 1], [-1, -2]).shape (5, 4, 3) >>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape (5, 4, 3)
2.expand_dims(a, axis)
就是在axis的那一个轴上把数据加上去,这个数据在axis这个轴的0位置。
例如原本为一维的2个数据,axis=0,则shape变为(1,2),axis=1则shape变为(2,1)再例如 原本为 (2,3),axis=0,则shape变为(1,2,3),axis=1则shape变为(2,1,3)
numpy.
expand_dims
(
a,
axis
)
[source]
Expand the shape of an array.
Insert a new axis that will appear at the axis position in the expanded array shape.
Note
Previous to NumPy 1.13.0, neither axis < -a.ndim - 1
nor axis > a.ndim
raised errors or put the new axis where documented. Those axis values are now deprecated and will raise an AxisError in the future.
Parameters: | a : array_like
axis : int
|
---|---|
Returns: | res : ndarray
|
See also
squeeze
reshape
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[:,np.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