달나라 노트

Python numpy : concatenate (array 합치기) 본문

Python/Python numpy

Python numpy : concatenate (array 합치기)

CosmosProject 2021. 3. 29. 23:45
728x90
반응형

 

 

 

numpy의 concatenate는 list나 array등을 하나의 list또는 array로 합쳐줍니다.

 

import numpy as np

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

print(np.concatenate([x, y]))


-- Result
[1 2 3 4 5 1 2 3 4 5]

위 예시는 2개의 list를 합친 것입니다.

 

 

 

 

 

import numpy as np

x = np.array([
    [1, 2],
    [3, 4]
])
y = np.array([
    [5, 6],
    [7, 8],
    [9, 10]
])

print(np.concatenate([x, y]))



-- Result
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]]

위 예시는 2개의 array를 하나로 합친 것입니다.

 

 

import numpy as np

x = np.array([
    [1, 2],
    [3, 4]
])
y = np.array([
    [
        [5, 6],
        [7, 8],
        [9, 10]
    ]
])

print(np.concatenate([x, y]))

 

 

import numpy as np

x = np.array([
    [1, 2],
    [3, 4]
])
y = np.array([
    [5, 6],
    [7, 8],
    [9, 10, 11]
])

print(np.concatenate([x, y]))

 

여기서 주의할 것은 합칠 list나 array의 차원(demention)이 동일해야한다는 것입니다.

위 2개의 예시처럼 차원이 다른 array를 합치려고하면 error가 발생합니다.

 

 

 

 

 

 

728x90
반응형
Comments