itemsize
itemsize says how many bytes a single element takes up.
In this page:
Syntax
arr.itemsize
itemsize
itemsize is the number of bytes per element, determined by dtype. int64 and float64 use 8 bytes, int32 and float32 use 4, and int8 uses 1. Choosing a smaller dtype directly reduces memory.
Note:
Bool arrays use 1 byte per element, not 1 bit.
Example: itemsize
import numpy as np
for dt in (np.int8, np.int32, np.int64, np.float32, np.float64):
print(np.dtype(dt).name, np.zeros(1, dtype=dt).itemsize)
# Output:
# int8 1
# int32 4
# int64 8
# float32 4
# float64 8
Related Topics
Common Mistakes
- Confusing bytes with bits
- Assuming all ints are 4 bytes
- Ignoring itemsize when memory is tight
Chapter Summary
- itemsize is bytes per element
- It depends on dtype
- int64 and float64 are 8 bytes
- Smaller dtypes save memory