오답노트

[Numpy] 배열 분할 본문

Python/Numpy

[Numpy] 배열 분할

권멋져 2022. 7. 14. 19:23

vsplit / hsplit

vsplit 함수는 행으로 배열을 나누고, hsplit 함수는 열로 배열을 나눈다.

 

2차원 배열 분할

#2차원 배열 분할
arr = np.arange(1,13).reshape(3,4)
print(arr)
'''

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

'''

# vsplit => axis=0
arr_v = np.vsplit(arr,3)
print(arr_v)
'''
[array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]]), array([[ 9, 10, 11, 12]])]

'''

#hsplit => axis=1
arr_h = np.hsplit(arr,4)
print(arr_h)
'''

[array([[1],
       [5],
       [9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]]), array([[ 4],
       [ 8],
       [12]])]


'''

 

3차원 배열 분할

#3차원 배열 분할
arr = np.random.randint(0,10,[4,6,8])
print(arr)

'''

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

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

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

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

'''

arr_v = np.vsplit(arr,2)
print(arr_v)
'''

[array([[[7, 2, 5, 6, 0, 0, 3, 6],
        [8, 7, 1, 3, 5, 5, 6, 6],
        [3, 7, 7, 0, 2, 7, 3, 9],
        [4, 0, 8, 4, 9, 4, 0, 1],
        [5, 0, 9, 1, 8, 9, 8, 8],
        [4, 2, 5, 8, 3, 6, 3, 5]],

       [[6, 8, 1, 3, 5, 0, 8, 2],
        [4, 4, 0, 2, 9, 1, 9, 0],
        [6, 2, 5, 2, 9, 5, 0, 4],
        [2, 0, 1, 9, 3, 6, 0, 7],
        [6, 4, 9, 4, 3, 8, 7, 0],
        [7, 0, 2, 2, 4, 7, 9, 7]]]), 
        
array([[[0, 0, 3, 8, 4, 5, 9, 7],
        [0, 3, 8, 5, 4, 8, 3, 2],
        [2, 2, 3, 8, 5, 6, 2, 1],
        [7, 8, 1, 4, 6, 9, 3, 7],
        [6, 7, 9, 4, 6, 8, 9, 0],
        [2, 1, 1, 1, 4, 8, 2, 7]],

       [[3, 4, 6, 0, 5, 1, 1, 1],
        [9, 2, 8, 1, 1, 8, 2, 2],
        [7, 6, 3, 4, 9, 5, 2, 5],
        [9, 8, 0, 4, 3, 0, 1, 0],
        [0, 3, 7, 6, 8, 5, 8, 1],
        [9, 5, 9, 7, 0, 8, 4, 6]]])]

'''


arr_h = np.hsplit(arr,2)
print(arr_h)
'''
[array([[[7, 2, 5, 6, 0, 0, 3, 6],
        [8, 7, 1, 3, 5, 5, 6, 6],
        [3, 7, 7, 0, 2, 7, 3, 9]],

       [[6, 8, 1, 3, 5, 0, 8, 2],
        [4, 4, 0, 2, 9, 1, 9, 0],
        [6, 2, 5, 2, 9, 5, 0, 4]],

       [[0, 0, 3, 8, 4, 5, 9, 7],
        [0, 3, 8, 5, 4, 8, 3, 2],
        [2, 2, 3, 8, 5, 6, 2, 1]],

       [[3, 4, 6, 0, 5, 1, 1, 1],
        [9, 2, 8, 1, 1, 8, 2, 2],
        [7, 6, 3, 4, 9, 5, 2, 5]]]),
array([[[4, 0, 8, 4, 9, 4, 0, 1],
        [5, 0, 9, 1, 8, 9, 8, 8],
        [4, 2, 5, 8, 3, 6, 3, 5]],

       [[2, 0, 1, 9, 3, 6, 0, 7],
        [6, 4, 9, 4, 3, 8, 7, 0],
        [7, 0, 2, 2, 4, 7, 9, 7]],

       [[7, 8, 1, 4, 6, 9, 3, 7],
        [6, 7, 9, 4, 6, 8, 9, 0],
        [2, 1, 1, 1, 4, 8, 2, 7]],

       [[9, 8, 0, 4, 3, 0, 1, 0],
        [0, 3, 7, 6, 8, 5, 8, 1],
        [9, 5, 9, 7, 0, 8, 4, 6]]])]


'''

'Python > Numpy' 카테고리의 다른 글

[Numpy] 배열 병합  (0) 2022.07.14
[Numpy] 배열의 원소 추가/삭제  (0) 2022.07.14
[Numpy] 배열의 전치행렬  (0) 2022.07.14
[Numpy] 배열의 형태 변경  (0) 2022.07.14
[Numpy] 배열의 정렬  (0) 2022.07.11