Skip to main content

transpose

Returns an array with axes transposed.
numpy.transpose(a, axes=None)
a
array_like
Input array.
axes
tuple or list of ints
default:"None"
If 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. The i-th axis of the returned array will correspond to the axis numbered axes[i] of the input. If not specified, defaults to range(a.ndim)[::-1], which reverses the order of the axes.
Returns: ndarray - a with its axes permuted. A view is returned whenever possible.

Examples

import numpy as np

# 2D array transpose
a = np.array([[1, 2], [3, 4]])
print(np.transpose(a))
# array([[1, 3],
#        [2, 4]])

# 3D array with specified axes
x = np.ones((1, 2, 3))
print(np.transpose(x, (1, 0, 2)).shape)
# (2, 1, 3)

# Reverse all axes (default behavior)
print(np.transpose(x).shape)
# (3, 2, 1)

See Also

  • ndarray.T - Shorthand for transpose
  • moveaxis - Move axes to new positions
  • swapaxes - Interchange two axes

ndarray.T

View of the transposed array.
ndarray.T
For a 1-D array, this returns an unchanged view of the original array. For a 2-D array, this is the standard matrix transpose. For an n-D array, if axes are not provided, it reverses the order of the axes.

Examples

import numpy as np

a = np.array([[1, 2], [3, 4]])
print(a.T)
# array([[1, 3],
#        [2, 4]])

# 1-D array returns unchanged
x = np.array([1, 2, 3])
print(x.T)
# array([1, 2, 3])

# Multi-dimensional
y = np.ones((2, 3, 4))
print(y.T.shape)
# (4, 3, 2)

See Also

  • transpose - Permute the dimensions of an array
  • ndarray.transpose - Equivalent method with more options

swapaxes

Interchange two axes of an array.
numpy.swapaxes(a, axis1, axis2)
a
array_like
Input array.
axis1
int
First axis.
axis2
int
Second axis.
Returns: ndarray - A view of the array with axes swapped (when possible).

Examples

import numpy as np

# Simple 2D swap
x = np.array([[1,2,3]])
print(np.swapaxes(x, 0, 1))
# array([[1],
#        [2],
#        [3]])

# 3D array swap
x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]])
print(x.shape)
# (2, 2, 2)

print(np.swapaxes(x, 0, 2).shape)
# (2, 2, 2)

print(np.swapaxes(x, 0, 2))
# array([[[0, 4],
#         [2, 6]],
#        [[1, 5],
#         [3, 7]]])

See Also

  • transpose - Permute axes of an array
  • moveaxis - Move axes to new positions

moveaxis

Move axes of an array to new positions.
numpy.moveaxis(a, source, destination)
a
ndarray
The array whose axes should be reordered.
source
int or sequence of int
Original positions of the axes to move. These must be unique.
destination
int or sequence of int
Destination positions for each of the original axes. These must also be unique.
Returns: ndarray - Array with moved axes. This array is a view of the input array.

Examples

import numpy as np

x = np.zeros((3, 4, 5))

# Move axis 0 to position -1 (last)
print(np.moveaxis(x, 0, -1).shape)
# (4, 5, 3)

# Move axis -1 to position 0 (first)
print(np.moveaxis(x, -1, 0).shape)
# (5, 3, 4)

# Move multiple axes
print(np.moveaxis(x, [0, 1], [-1, -2]).shape)
# (5, 4, 3)

# Equivalent to transpose
print(np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape)
# (5, 4, 3)
print(np.transpose(x).shape)
# (5, 4, 3)

See Also

  • transpose - Permute the dimensions of an array
  • swapaxes - Interchange two axes of an array

Build docs developers (and LLMs) love