Numpy - numpy.transpose

numpy.transpose(a, axes=None)

Explain

For an array a with two axes, transpose(a) gives the matrix transpose.

축이 두 개인 배열 a(2차원 행렬)의 경우 transpose(a)는 행렬 변환(transpose, 행과 열을 바꾼다)을 반환합니다.

Parameters explain

  • a : array_like

    Input array.

    a는 2차원 이상의 배열입니다.

  • axes : tuple or list of ints, optional

    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. 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.

    axes를 파라미터로 전달할 경우 axis(축)을 선택적으로 변환할 수 있습니다. 특정하지 않을 경우 축을 반대로 변환합니다.

Reference

배열의 축(axis) 이해하기을 읽어보면 축 개념을 쉽게 이해할 수 있다.

Example

x = np.ones((1, 2))
print(f"x: {x.shape}")
# x: (1, 2)
x_t1 = np.transpose(x, (1, 0))
print(f"x_t1: {x_t1.shape}")
# x_t1: (2, 1)

y = np.ones((1, 2, 3))
print(f"y: {y.shape}")
# y: (1, 2, 3)
y_t1 = np.transpose(y, (2, 1, 0))
print(f"y_t1: {y_t1.shape}")
# y_t1: (3, 2, 1)

y_t2 = np.transpose(y, (1, 2, 0))
print(f"y_t2: {y_t2.shape}")
# y_t2: (2, 3, 1)

z = np.ones((1, 2, 3, 4))
print(f"z: {z.shape}")
# z: (1, 2, 3, 4)

z_t1 = np.transpose(z, (3, 2, 1, 0))
print(f"z_t1: {z_t1.shape}")
# z_t1: (4, 3, 2, 1)

z_t2 = np.transpose(z, (3, 1, 2, 0))
print(f"z_t2: {z_t2.shape}")
# z_t2: (4, 2, 3, 1)