shape
The shape tells you how many rows, columns and so on the array has.
In this page:
Syntax
arr.shape # (rows, cols)
shape
The shape attribute is a tuple giving the length of each dimension. A 2-by-3 matrix has shape (2, 3). Assigning to shape or using reshape changes the layout without altering the data.
Note:
A 1-D array of length 5 has shape (5,) with a trailing comma, because it is a tuple.
Example: shape
import numpy as np
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape)
print("rows:", m.shape[0], "cols:", m.shape[1])
print(np.array([1, 2, 3]).shape)
# Output:
# (2, 3)
# rows: 2 cols: 3
# (3,)
Related Topics
Common Mistakes
- Forgetting shape is a tuple, not a number
- Confusing (5,) with (5, 1)
- Treating shape as a method and calling it
Chapter Summary
- shape is a tuple of dimension sizes
- It is an attribute so no parentheses
- 1-D shapes look like (n,)
- Product of shape equals size