Python/Numpy
[공개][np.stack설명] 넘파이numpy의 stack에 대한 graphical 설명
uniqueone
2023. 6. 15. 18:02
넘파이의 3차원에서 axis는 아래 그림과 같이 axis=0은 깊이, axis=1은 세로축, axis=2는 가로축이다.
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
일 때
1. np.stack((a, b), axis=0)
np.stack((a, b), axis=0)
# array([[[1, 2],
# [3, 4]],
# [[5, 6],
# [7, 8]]])
그림은 아래와 같다. axis=0이므로 reshape에서 1번째에 1이 추가된다.
2. np.stack((a, b), axis=1)
np.stack((a, b), axis=1)
# array([[[1, 2],
# [5, 6]],
# [[3, 4],
# [7, 8]]])
그림은 아래와 같다. axis=1이므로 reshape에서 2번째에 1이 추가된다.
3. np.stack((a, b), axis=2)
np.stack((a, b), axis=2)
# array([[[1, 5],
# [2, 6]],
# [[3, 7],
# [4, 8]]])
그림은 아래와 같다. axis=2이므로 reshape에서 3번째에 1이 추가된다.
참고로, 아래 그림은 np.reshape을 이용해 shape=(2,3,2) --> shape=(2,3,2)으로 바뀌는 과정을 나타낸다. 가장 바깥쪽(3번째 axis)이 먼저 unrolling되고, rolling될 때도 가장 바깥쪽(3번째 axis)이 먼저 rolling된다. (https://towardsdatascience.com/np-reshape-in-python-39b4636d7d91 참조)