Deep Learning

[Numpy] Array(배열)의 속성 [ndim, shape, size, dtype, itemsize, nbytes]

잇뉴얼 2022. 7. 7. 04:04
728x90
반응형

[Numpy] Array(배열)의 속성 [ndim, shape, size, dtype, itemsize, nbytes]


  • numpy의 배열은 동일한 타입의 값을 가진다.
  • 배열의 차원을 rank라고 한다.

numpy의 배열을 생성하고, 기본적인 정보(속성)를 확인할 수 있다.

# 모듈 numpy 추가
import numpy as np

# 0~9사이의 랜덤값을 1차원 배열에 3개의 값을 넣어 생성한다.
x1 = np.random.randint(10, size= 3)
# 0~9사이의 랜덤값을 2차원 배열(3x4)에 값을 넣어 생성한다.
x2 = np.random.randint(10, size = (3,4))
# 0~9사이의 랜덤값을 3개의 2차원 배열(4x5)[3차원 배열]에 값을 넣어 생성한다.
# 여기서 size = (3,4,5)중 3의 값은 깊이를 표현한다.
x3 = np.random.randint(10, size = (3,4,5))

print(x1)
# 결과 : [2 7 0]

print(x2)
print(x2.ndim)
print(x2.shape)
print(x2.size)
print(x2.dtype)
print(x2.itemsize) 
print(x2.nbytes)
# 결과 : 
[[9 3 3 0]
 [6 9 1 1]
 [4 7 9 0]]
2
(3, 4)
12
int64
8
96

print(x3)
print(x3.ndim)
print(x3.shape)
print(x3.size)
print(x3.dtype)
print(x3.itemsize) 
print(x3.nbytes)
# 결과 : 
[[[4 7 4 9 6]
  [4 1 0 8 8]
  [7 7 6 4 8]
  [0 2 9 6 0]]

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

 [[9 3 2 8 6]
  [9 4 6 4 6]
  [9 1 9 8 1]
  [1 9 7 3 4]]]
3
(3, 4, 5)
60
int64
8
480
  • ndim : 배열의 차원수 혹은 배열의 축 수
  • shape : 배열의 각 차원의 크기를 튜플로 표현
  • size : 배열안에 데이터 갯수
  • dtype : 배열 원소의 데이터 타입
  • itemsize : 배열의 각 요소의 바이트 단위 크기 확인
  • nbytes : 배열의 데이터가 어느 위치에 메모리에 저장되어있는지 확인
반응형