The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →For a two-dimensional NumPy array, array.shape is a tuple in the order (rows, columns). That means array.shape[0] is the row count and array.shape[1] is the column count. The values are tuple entries, not special NumPy methods.
What does a NumPy array’s shape tuple mean?
NumPy’s ndarray.shape is a tuple of non-negative integers: each entry gives the length of the array along one axis. In a two-dimensional, matrix-like array, those axis lengths are conventionally read as rows followed by columns. NumPy’s ndarray documentation shows a 2-by-3 array with shape (2, 3).
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print(arr.shape) # (2, 3)
print(arr.shape[0]) # 2 rows
print(arr.shape[1]) # 3 columns
Python tuples use zero-based indexing, so index 0 selects the first entry and index 1 selects the second. NumPy’s beginner guide also explains how to inspect an array’s shape and size.
When are shape[0] and shape[1] valid?
A shape tuple has one entry per dimension, so an index is valid only if that position exists.
#1 Best Overall
- A one-dimensional array with four elements has shape
(4,). Its shape has only one entry:shape[0]is valid, butshape[1]raisesIndexError. The trailing comma is Python’s notation for a one-item tuple. - A two-dimensional array has two entries, so
shape[0]andshape[1]are valid. - A three-dimensional array with shape
(2, 3, 4)has three axis lengths, accessible asshape[0],shape[1], andshape[2].
If an input’s dimensionality may vary, check arr.ndim before accessing a particular axis, or test whether len(arr.shape) is large enough. NumPy documents that arr.ndim equals len(arr.shape); see its shape reference.
How shape differs from ndim and size
These attributes answer different questions:
shapegives the length along each axis as a tuple.ndimgives the number of axes, which is also the number of entries inshape.sizegives the total number of elements. For shape(3, 4), the array has 12 elements.
What happens to shape when you transpose an array?
Transposing a two-dimensional array swaps its axes. For example, an array with shape (3, 4) has shape (4, 3) after its rows and columns are exchanged. NumPy illustrates this in its quickstart guide. This is why a shape entry describes an axis position, rather than a permanent label attached to the underlying data.
Quick Recap
Best Value
Rank #2
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




