bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / transpose
_ArrayFunctionDispatcher
numpy:transpose
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/_core/fromnumeric.py :604
Signature
def transpose ( a , axes = None ) Summary
Returns an array with axes transposed.
Extended Summary
For a 1-D array, this returns an unchanged view of the original array, as a transposed vector is simply the same vector. To convert a 1-D array into a 2-D column vector, an additional dimension must be added, e.g., np.atleast_2d(a).T achieves this, as does a[:, np.newaxis]. For a 2-D array, this is the standard matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided, then transpose(a).shape == a.shape[::-1].
Parameters
a: array_likeInput array.
axes: tuple or list of ints, optionalIf specified, it must be a tuple or list which contains a permutation of [0, 1, ..., N-1] where N is the number of axes of
a. Negative indices can also be used to specify axes. The i-th axis of the returned array will correspond to the axis numberedaxes[i]of the input. If not specified, defaults torange(a.ndim)[::-1], which reverses the order of the axes.
Returns
p: ndarrayawith its axes permuted. A view is returned whenever possible.
Notes
Use transpose(a, argsort(axes)) to invert the transposition of tensors when using the axes keyword argument.
Examples
import numpy as np a = np.array([[1, 2], [3, 4]]) a np.transpose(a)✓
a = np.array([1, 2, 3, 4]) a np.transpose(a)✓
a = np.ones((1, 2, 3)) np.transpose(a, (1, 0, 2)).shape✓
a = np.ones((2, 3, 4, 5)) np.transpose(a).shape✓
a = np.arange(3*4*5).reshape((3, 4, 5)) np.transpose(a, (-1, 0, -2)).shape✓
See also
- argsort
Return the indices that would sort an array.
- moveaxis
Move axes of an array to new positions.
- ndarray.transpose
Equivalent method.
Aliases
-
numpy.permute_dims