Python/numpy
(numpy) 타입변환 (np.astype, np.tolist)
re-hwi
2022. 10. 14. 12:56
np.astype (x)
: 데이터 타입 변환 매개변수에 원하는 데이터 타입을 지정해 변환한다.
예시)
import numpy as np
a = np.array([1,2,3])
print('a = ',a.dtype)
b = a.astype(float)
print('b = ',b.dtype)
>>> a = int32
b = float64
int 형식인 a 배열을 float 타입으로 바꿔준다.
np.tolist (array)
: array 형식인 데이터를 list 형식으로 바꾸어준다.
예시)
import numpy as np
a = np.array([1,2,3,4])
print(type(a))
b = a.tolist()
print(type(b))
>>> <class 'numpy.ndarray'>
<class 'list'>
a 의 형태는 array이지만 tolist로 바꿔준 후 list 타입으로 바뀌었다.
반응형