donut

파이썬 3차원 배열 다루기

Module/Pandas
numpystudy

Numpy Study

3차원 넘피 배열에 대한 공부 기록 입니다.

In [30]:
#모듈선언
import numpy as np
In [31]:
#3차원 배열 선언
test1 = np.array([[[0,1,2],[3,4,5],[6,7,8]],
                  [[9,10,11],[12,13,14],[15,16,17]],
                  [[18,19,20],[21,22,23],[24,25,26]]])
In [32]:
#데이터확인
test1
Out[32]:
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
In [33]:
#데이터 길이, 차원, 갯수(크기)확인
print(test1.shape)
print(test1.ndim)
print(test1.size)
(3, 3, 3)
3
27
In [34]:
test1.reshape(9,3)
Out[34]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17],
       [18, 19, 20],
       [21, 22, 23],
       [24, 25, 26]])
In [35]:
#3차원의 배열을 1차원으로 재배치
test1.reshape(27,)
Out[35]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26])
In [36]:
#3차원의 배열을 2차원으로 재배치
test1.reshape(3,9)
Out[36]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23, 24, 25, 26]])
In [37]:
#3차원의 배열을 재배치
test1.reshape(3,1,9)
Out[37]:
array([[[ 0,  1,  2,  3,  4,  5,  6,  7,  8]],

       [[ 9, 10, 11, 12, 13, 14, 15, 16, 17]],

       [[18, 19, 20, 21, 22, 23, 24, 25, 26]]])
In [38]:
#3차원의 배열을 1차원으로 재배치
test1.flatten()
Out[38]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26])
In [ ]:
 

'Module > Pandas' 카테고리의 다른 글

Python Pandas DataFrame  (0) 2020.07.19
Python pandas series  (0) 2020.07.19