Table of Contents
ToggleThe itemsize attribute in a NumPy array indicates the size, in bytes, of each element in the array. This size is determined by the data type of the array elements (e.g., integer, float).
By knowing the itemsize, you can estimate the total memory consumption of the array. This is important for understanding the memory layout and storage requirements of arrays, especially when dealing with large datasets.
You can access the itemsize of a NumPy array using the itemsize attribute. This attribute returns an integer representing the size (in bytes) of each element in the array.
In the following example, we are accessing the itemsize of an integer array and a float array −
# Open Compiler
import numpy as np
# Creating arrays with different data types
array_int32 = np.array([1, 2, 3], dtype=np.int32)
array_float64 = np.array([1.0, 2.0, 3.0], dtype=np.float64)
# Checking itemsize
print("Itemsize of int32 array:", array_int32.itemsize)
print("Itemsize of float64 array:", array_float64.itemsize)
Output:
Following is the output obtained −
Itemsize of int32 array: 4
Itemsize of float64 array: 8
To calculate the total memory occupied by the array, you can multiply the “itemsize” by the total number of elements in the array.
For example, if an array has “1000” elements and an itemsize of “8” bytes, the total memory used by the array would be “1000 * 8” = “8000” bytes.
In this example, we are calculating the total memory usage of a 2D array −
# Open Compiler
import numpy as np
# Create a 2D array
array_2d = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Calculate total memory usage
total_memory_usage = array_2d.size * array_2d.itemsize
print(f"Total memory usage: {total_memory_usage} bytes")
Output:
This will produce the following result −
Total memory usage: 16 bytes
In NumPy, different data types have different itemsize values. For example −
np.int8 has an itemsize of 1 byte.np.int16 has an itemsize of 2 bytes.np.float64 has an itemsize of 8 bytes.np.complex128 has an itemsize of 16 bytes.In the example below, we are creating arrays with different data types and then checking the itemsize of each array −
# Open Compiler
import numpy as np
# Creating arrays with different data types
array_int8 = np.array([1, 2, 3], dtype=np.int8)
array_int16 = np.array([1, 2, 3], dtype=np.int16)
array_uint32 = np.array([1, 2, 3], dtype=np.uint32)
array_float16 = np.array([1.0, 2.0, 3.0], dtype=np.float16)
array_complex128 = np.array([1+2j, 3+4j, 5+6j], dtype=np.complex128)
# Checking itemsize
print("Itemsize of int8 array:", array_int8.itemsize)
print("Itemsize of int16 array:", array_int16.itemsize)
print("Itemsize of uint32 array:", array_uint32.itemsize)
print("Itemsize of float16 array:", array_float16.itemsize)
print("Itemsize of complex128 array:", array_complex128.itemsize)
Output:
The output obtained is as shown below −
Itemsize of int8 array: 1
Itemsize of int16 array: 2
Itemsize of uint32 array: 4
Itemsize of float16 array: 2
Itemsize of complex128 array: 16
By modifying the data type of the array, you can change its itemsize, which affects how much memory each element occupies and, consequently, the total memory usage of the array.
You can change the data type of a NumPy array using the astype() function. This function accepts the target data type to which you want to convert the array.
In the following example, we are changing the data type of an array from “int32” to “int8” and checking the itemsize of that array −
# Open Compiler
import numpy as np
# Original array with int32
array_original = np.array([1, 2, 3], dtype=np.int32)
print(f"Original itemsize: {array_original.itemsize} bytes")
# Change data type to int8
array_new = array_original.astype(np.int8)
print(f"New itemsize: {array_new.itemsize} bytes")
Output:
After executing the above code, we get the following output −
Original itemsize: 4 bytes
New itemsize: 1 byte
Key Takeaway: Master NumPy array itemsize for efficient memory management at Vista Academy!
