NumPy data types are used to define the type of data stored in a NumPy array. Unlike Python’s built - in data types, NumPy data types are more specific and optimized for numerical operations. Each data type has a unique identifier, such as int8
, float32
, etc.
int8
, int16
, int32
, int64
(signed integers) and uint8
, uint16
, uint32
, uint64
(unsigned integers). The number after int
or uint
represents the number of bits used to store the integer. For example, int8
uses 8 bits and can store values from - 128 to 127, while uint8
can store values from 0 to 255.float16
, float32
, float64
. Floating - point types are used to represent real numbers. float32
is a single - precision floating - point number, and float64
is a double - precision floating - point number.bool
is used to represent True or False values.complex64
and complex128
are used to represent complex numbers. complex64
consists of two 32 - bit floating - point numbers (real and imaginary parts), and complex128
consists of two 64 - bit floating - point numbers.import numpy as np
# Create an array of integers
int_array = np.array([1, 2, 3], dtype=np.int32)
print("Integer array:", int_array)
print("Data type of integer array:", int_array.dtype)
# Create an array of floating - point numbers
float_array = np.array([1.1, 2.2, 3.3], dtype=np.float64)
print("Floating - point array:", float_array)
print("Data type of floating - point array:", float_array.dtype)
# Create an array of boolean values
bool_array = np.array([True, False, True], dtype=np.bool)
print("Boolean array:", bool_array)
print("Data type of boolean array:", bool_array.dtype)
import numpy as np
original_array = np.array([1, 2, 3])
print("Original array:", original_array)
print("Original data type:", original_array.dtype)
# Change the data type to float64
new_array = original_array.astype(np.float64)
print("New array:", new_array)
print("New data type:", new_array.dtype)
uint8
instead of int32
to save memory.import numpy as np
# Using int32
large_int_array = np.array([1, 2, 3], dtype=np.int32)
print("Memory used by int32 array:", large_int_array.nbytes, "bytes")
# Using uint8
small_int_array = np.array([1, 2, 3], dtype=np.uint8)
print("Memory used by uint8 array:", small_int_array.nbytes, "bytes")
float64
is the default choice as it provides high precision. However, if you are working on a large - scale project where performance is critical and you can tolerate some loss of precision, float32
can be used as it requires less memory and is faster in some operations.import numpy as np
# Explicitly specify data type
explicit_array = np.array([1, 2, 3], dtype=np.int16)
print("Explicitly typed array:", explicit_array)
import numpy as np
my_array = np.array([1.0, 2.0, 3.0])
if my_array.dtype != np.float64:
print("Unexpected data type!")
NumPy data types are a powerful and essential feature for efficient numerical computing in Python. By understanding the fundamental concepts, usage methods, common practices, and best practices of NumPy data types, you can optimize your code for memory usage, performance, and accuracy. Whether you are a beginner or an experienced data scientist, mastering NumPy data types will significantly enhance your ability to work with numerical data.