Understanding Dtype
The dtype
attribute is particularly important because it determines the type of data stored in the array. NumPy supports various data types, such as integers (int8
, int16
, int32
, int64
), unsigned integers (uint8
, uint16
, uint32
, uint64
), floating-point numbers (float16
, float32
, float64
), and complex numbers (complex64
, complex128
).
When creating a NumPy array, you can specify the dtype
using the dtype
parameter. If not specified, NumPy will try to infer the data type from the input data.
Dtype Usage
Let's explore using the dtype
attribute
## Create a float array from a list
float_array = np.array([1.2, 2.3, 3.4, 4.5], dtype=np.float32)
print("Float array dtype:", float_array.dtype) ## Output: float32
## Create an integer array from a list
int_array = np.array([1, 2, 3, 4, 5], dtype=np.int16)
print("Integer array dtype:", int_array.dtype) ## Output: int16
## Create a complex array from a list
complex_array = np.array([1 + 2j, 2 + 3j, 3 + 4j], dtype=np.complex64)
print("Complex array dtype:", complex_array.dtype) ## Output: complex64
## Create an array and let Numpy infer the data type
mixed_array = np.array([1, 2, 3.5, 4.5])
print("Mixed array dtype:", mixed_array.dtype) ## Output: float64
## Changing the data type of an existing array
new_dtype_array = mixed_array.astype(np.int32)
print("New dtype array:", new_dtype_array) ## Output: [1 2 3 4]
print("New dtype:", new_dtype_array.dtype) ## Output: int32
## Creating an array of zeros with specified dtype
zeros_array = np.zeros((3, 3), dtype=np.uint8)
print("Zeros array with dtype uint8:\n", zeros_array) ## Output:[[0 0 0] [0 0 0] [0 0 0]]