In NumPy, the shape of an array refers to the dimensions (or the size) of each axis (or dimension) of the array. It tells you how many elements are along each axis. The shape of an array is represented as a tuple of integers, where each integer represents the size of the corresponding dimension.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
Here, arr has a shape of (2, 3) because it has 2 rows and 3 columns.
print(arr.shape) # Output: (2, 3)
Here are a few key points about Array shapes
Rank : The number of dimensions in the array is also known as its rank. For example, a 1-dimensional array has a rank of 1, a 2-dimensional array has a rank of 2, and so on.
Size : The total number of elements in the array is the product of the sizes of all its dimensions. For example, in the array [[1, 2, 3], [4, 5, 6]], the size is 2 (rows) * 3 (columns) = 6.
Reshaping You can change the shape of an array using the reshape() method. However, the total number of elements must remain the same. For example, you can reshape a (6,) array into a (2, 3) array.
Array Operations : When performing operations on arrays, NumPy automatically broadcasts arrays with different shapes if possible. Broadcasting allows you to perform operations even when the shapes of the arrays don't match exactly.