Before operating on an array, you inspect it. Every ndarray exposes a handful of attributes that tell you its structure and memory footprint — the first things you check when a shape mismatch or memory issue appears.
The key attributes
| Attribute | Meaning |
|---|---|
ndim | Number of dimensions (axes) |
shape | Size along each axis, e.g. (2, 3) |
size | Total number of elements |
dtype | Element type |
itemsize | Bytes per element |
nbytes | Total bytes (size × itemsize) |
a = np.zeros((3, 4), dtype=np.float64)a.ndim # 2a.shape # (3, 4)a.size # 12a.itemsize # 8 (float64 = 8 bytes)a.nbytes # 96 (12 × 8)
Shape is your debugger
Key takeaways
- 01ndim, shape and size describe an array's structure.
- 02dtype, itemsize and nbytes describe its memory footprint.
- 03nbytes = size × itemsize — how to estimate an array's RAM cost.
- 04Check .shape first when debugging — most NumPy bugs are shape mismatches.
