Numpy 배열 ndarray는 dtype으로 저장되는데, 이 dtype은 np.array()로 ndarray를 생성할 때 지정하거나 astype() method로 변경할 수 있다.

 

ndarray.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)

: ndarray의 dtype을 변경하는 함수

import numpy as np 

a = np.array([1, 2, 3], dtype=np.int64) 
print(a.dtype) # [1, 2, 3]
print(a) # int64

a = a.astype(np.float32) # 꼭 재할당을 해줘야한다
print(a.dtype) # float32
print(a) # [1. 2. 3.]

 

* dtype의 유형

int8, int16, int32, int64
unit8, unit16, unit32, unit64
float16, float32, float128
complex64, complex128, complex256
bool, unicode, object

 

'ML&AI > 관련 python, lib 문법' 카테고리의 다른 글

Ellipsis 객체, '...' in Python  (0) 2021.01.27
np.linspace()  (0) 2021.01.27
np.random.rand()  (0) 2021.01.27

+ Recent posts