NumPy Tutorial-NumPy 데이터 유형 및 변환
    
    Jinku Hu
    2025년2월18일
    
    
이 기사와 함께 볼 수 있도록 자세한 유튜브 영상을 제작하여 아래에 삽입했습니다!
 
데이터 타입-NumPy 의 dtype 은 파이썬의 프리미티브 데이터 타입과 다릅니다. 예를 들어 dtype 은 데이터 계산에 유용한 해상도가 더 높은 타입을 가지고 있습니다.
NumPy 데이터 유형
| 데이터 형식 | 기술 | 
|---|---|
| bool | 부울 | 
| int8 | 8 비트 부호있는 정수 | 
| int16 | 16 비트 부호있는 정수 | 
| int32 | 32 비트 부호있는 정수 | 
| int64 | 64 비트 부호있는 정수 | 
| uint8 | 부호없는 8 비트 정수 | 
| uint16 | 16 비트 부호없는 정수 | 
| uint32 | 부호없는 32 비트 정수 | 
| uint64 | 부호없는 64 비트 정수 | 
| float16 | 16 비트 부동 소수점 수 | 
| float32 | 32 비트 부동 소수점 수 | 
| float64 | 64 비트 부동 소수점 수 | 
| complex64 | 64 비트 복소수 | 
| complex128 | 128 비트 복소수 | 
새로운 ndarray데이터를 생성 할 때 NumPy 라이브러리에서 문자열 또는 데이터 유형 상수로 요소의 데이터 유형을 정의 할 수 있습니다.
import numpy as np
# by string
test = np.array([4, 5, 6], dtype="int64")
# by data type constant in numpy
test = np.array([7, 8, 8], dtype=np.int64)
데이터 타입 변환
데이터 인스턴스가 생성 된 후 astype()메소드를 사용하여 요소 유형을 다른 유형으로 변경할 수 있습니다 (예 : 정수에서 부동으로).
>>> import numpy as np
>>> test = np.array([11, 12, 13, 14], dtype="int32")
>>> x = test.astype('float32')
>>> x
array([11., 12., 13., 14.], dtype=float32)
>>> test, test.dtype
(array([11, 12, 13, 14]), dtype('int32'))
주의
    데이터 형식 변환 방법은 새 배열 인스턴스 만 반환하며 원래 배열 인스턴스의 데이터와 정보는 변경되지 않았습니다.
    작가: Jinku Hu
    Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook