jinseon's log

[엘카데미] 엘카데미 챌린지_실습으로 배우는 Numpy_3일차 본문

ML & DL/엘카데미

[엘카데미] 엘카데미 챌린지_실습으로 배우는 Numpy_3일차

J_SEON 2023. 7. 19. 19:20

 

모양 바꾸기

- 배열.reshape

x = np.arange(8)

x.shape >> (8,)

x2 = x.reshape((2, 4)) >> [[0, 1, 2, 3],[4, 5, 6, 7]]
x2.shape >> (2, 4)

 

이어 붙이기

- np.concatenate

    - axis=0  : 행|밑으로

    - axis=1 : 열|옆으로

x = np.array([0, 1, 2])
y = np.array([3, 4, 5])

np.concatenate([x, y]) >> [0, 1, 2, 3, 4, 5]


# axis 축을 기준으로 붙이기

## axis=0 => 행|밑으로
matrix = np.arange(4).reshape(2, 2) >> [[0, 1], [2, 3]]
np.concatenate([matrix, matrix], axis=0) >> [[0, 1], [2, 3], [0, 1], [2, 3]]

## axis=1 => 열|옆으로
matrix = np.arange(4).reshape(2, 2) >> [[0, 1], [2, 3]]
np.concatenate([matrix, matrix], axis=1) >> [[0, 1, 0, 1], [2, 3, 2, 3]]

 

 

나누기

- np.split

# axis 축을 기준으로 분할

## axis=0 => 행|밑으로
matrix = np.arange(16).reshape(4, 4)
>> [[0, 1, 2, 3],
    [4, 5, 6, 7],
    [8, 9, 10, 11],
    [12, 13, 14, 15]]

upper, lower = np.split(matrix, [3], axis=0)
>> [[0, 1, 2, 3],
    [4, 5, 6, 7],
    [8, 9, 10, 11]]
   [12, 13, 14, 15]


## axis=1 => 열|옆으로
matrix = np.arange(16).reshape(4, 4)
>> [[0, 1, 2, 3],
    [4, 5, 6, 7],
    [8, 9, 10, 11],
    [12, 13, 14, 15]]

upper, lower = np.split(matrix, [3], axis=1)
>> [[0, 1, 2],     [[3],
    [4, 5, 6],      [7],
    [8, 9, 10],     [11],
    [12, 13, 14]]   [15]]

 

Comments