Thinking about the shape of NumPy arrays in the form of rows and columns will quickly let you down if you start working with more complicated data.
NumPy arrays are in fact multi-dimensional tensors. A tensor can have any number of dimensions. Vectors and matrices are in fact nothing but 1-D and 2-D tensors, respectively.
The shape of an array returns the size of each dimension of the tensor. Let's begin with a simple vector:
x = np.array([1, 2, 3])
Calling .shape
on this variable will return (3,)
since the array is a 1-D tensor with 3 elements in its first dimension. The comma is just a Python convention for indicating that the parenthesis represent a Python Tuple.
Now let's try a matrix:
m = np.array([
[1, 2, 3],
[4, 5, 6]
])
Running m.shape
now results in (2, 3)
since the first dimension of the tensor contains 2 vectors, and the second dimension (the vectors themselves) each contain 3 elements.
Finally, let's try a 3-D tensor:
t = np.array([
[
[1, 2, 3],
[4, 5, 6]
],
[
[7, 8, 9],
[0, 1, 2]
]
])
t.shape
will return (2, 2, 3)
which means that the first dimension of the array contains two elements (two matrices in this case), the second dimension (the matrices themsevles) contain two vectors each, and the third dimension (the vectors) each contain three elements.