관리 메뉴

Bull

[python/lib:numpy] np.stack()과 np.comcatenate() 본문

Computer Language/Python

[python/lib:numpy] np.stack()과 np.comcatenate()

Bull_ 2024. 4. 18. 08:43
import numpy as np

a = np.array([[1, 2], [3, 4]]) # ------ a.shape=(2, 2)
b = np.array([[5, 6], [7, 8]]) # ------ b.shape=(2, 2)

print('--------------concatenate, axis=0-----------------')
print(np.concatenate((a, b), axis=0)) # ------ shape=(4, 2)
print('--------------concatenate, axis=1-----------------')
print(np.concatenate((a, b), axis=1)) # ------ shape=(2, 4)
print('--------------stack, axis=0-----------------')
print(np.stack((a, b), axis=0)) # ------ shape=(2, 2, 2)
print('--------------stack, axis=1-----------------')
print(np.stack((a, b), axis=1)) # ------ shape=(2, 2, 2)
--------------concatenate, axis=0-----------------
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
--------------concatenate, axis=1-----------------
[[1 2 5 6]
 [3 4 7 8]]
--------------stack, axis=0-----------------
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
--------------stack, axis=1-----------------
[[[1 2]
  [5 6]]

 [[3 4]
  [7 8]]]

<concatenate, axis=0> → 행 병합

[[1 2]
 [3 4]
 [5 6]
 [7 8]]

<concatenate, axis=1> 열 병합

[[1 2 5 6]
 [3 4 7 8]]

<stack, axis=0> 스택처럼

[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

<stack, axis=1> 옆으로 눕히고 스택처럼

[[[1 2]
  [5 6]]

 [[3 4]
  [7 8]]]

엉성하지만  stack, axis=1은 대충 이런 느낌

 

 

참고자료


[도서] 딥러닝 파이토치 교과서 / 서지영 저자(글)

'Computer Language > Python' 카테고리의 다른 글

[Python] 유튜브 오디오 추출하기(.wav)  (0) 2024.05.26
[python:syntax] zip  (0) 2024.05.18