numpy.transpose#

numpy.transpose(a,axes=None)[source]#

Returns an array with axes transposed.

For a 1-D array, this returns an unchanged view of the original array, as atransposed vector is simply the same vector.To convert a 1-D array into a 2-D column vector, an additional dimensionmust be added, e.g.,np.atleast_2d(a).T achieves this, as doesa[:,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 theaxes are permuted (see Examples). If axes are not provided, thentranspose(a).shape==a.shape[::-1].

Parameters:
aarray_like

Input array.

axestuple or list of ints, optional

If specified, it must be a tuple or list which contains a permutationof [0, 1, …, N-1] where N is the number of axes ofa. Negativeindices can also be used to specify axes. The i-th axis of the returnedarray will correspond to the axis numberedaxes[i] of the input.If not specified, defaults torange(a.ndim)[::-1], which reversesthe order of the axes.

Returns:
pndarray

a with its axes permuted. A view is returned whenever possible.

See also

ndarray.transpose

Equivalent method.

moveaxis

Move axes of an array to new positions.

argsort

Return the indices that would sort an array.

Notes

Usetranspose(a,argsort(axes)) to invert the transposition of tensorswhen using theaxes keyword argument.

Examples

>>>importnumpyasnp>>>a=np.array([[1,2],[3,4]])>>>aarray([[1, 2],       [3, 4]])>>>np.transpose(a)array([[1, 3],       [2, 4]])
>>>a=np.array([1,2,3,4])>>>aarray([1, 2, 3, 4])>>>np.transpose(a)array([1, 2, 3, 4])
>>>a=np.ones((1,2,3))>>>np.transpose(a,(1,0,2)).shape(2, 1, 3)
>>>a=np.ones((2,3,4,5))>>>np.transpose(a).shape(5, 4, 3, 2)
>>>a=np.arange(3*4*5).reshape((3,4,5))>>>np.transpose(a,(-1,0,-2)).shape(5, 3, 4)
On this page