size
size is the total count of elements in the array, no matter how many dimensions it has.
In this page:
Syntax
arr.size
size
size multiplies all the shape entries together. A (3, 4) array has size 12. It is different from len(), which only returns the length of the first axis.
Note:
Use arr.size to check that a reshape target holds exactly the same number of elements.
Example: size
import numpy as np
m = np.zeros((3, 4))
print("size:", m.size)
print("len:", len(m))
print("shape product:", m.shape[0] * m.shape[1])
# Output:
# size: 12
# len: 3
# shape product: 12
Related Topics
Common Mistakes
- Using len() and expecting the total element count
- Reshaping to a shape whose product differs from size
- Calling size like a function
Chapter Summary
- size is the total number of elements
- It is the product of shape
- len returns only the first axis
- It is an attribute