Python Numpy 강좌 : 제 2강 - 배열 생성 (2)
1차원 배열
import numpy as np
a = np.array([1, 2, 3], dtype=int)
b = np.array([1.1, 2.2, 3.3], dtype=float)
c = np.array([1, 1, 0], dtype=bool)
print(a)
print(b)
print(c)
print(a.dtype)
- 결과
- [1 2 3]
[1.1 2.2 3.3]
[ True True False]
int32
numpy.array(배열, dtype=자료형)
을 사용하여 배열 생성과 자료형을 설정할 수 있습니다.
dtype
이 생략될 경우, 데이터의 자료형을 유추하여 자동적으로 dtype
을 할당합니다.
bool
형식의 특정한 배열을 생성시 dtype
을 사용하여 명시적
으로 표기합니다.
다차원 배열
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
print(a)
print(b)
print(a[0][1])
print(b[0][1][1])
- 결과
- [[1 2 3]
[4 5 6]
[7 8 9]]
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]]
2
4
numpy.array(다차원 배열, dtype=자료형)
을 사용하여 다차원 배열 생성과 자료형을 설정할 수 있습니다.
dtype
이 생략될 경우, 데이터의 자료형을 유추하여 자동적으로 dtype
을 할당합니다.
다차원 배열의 값을 불러올 때, 배열[페이지][행][열]...
형태로 배열의 값을 불러올 수 있습니다.
배열 속성 반환
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
print(a.ndim)
print(a.shape)
print(a.dtype)
print(np.ndim(b))
print(np.shape(b))
- 결과
- 2
(3, 3)
int32
3
(3, 2, 2)
배열.ndim
또는 np.ndim(배열)
을 사용하여 배열의 차원을 반환합니다.
배열.shape
또는 np.shape(배열)
을 사용하여 배열의 형태를 반환합니다.
배열.dtype
을 사용하여 배열의 자료형을 반환합니다.
공유하기


댓글 남기기