If the array x is declared as:
x = np.array([[1, 2], [3, 4]])
the shape of x is (2, 2) because it is a 2x2 matrix.
However, for a 1-dimensional vector, such as:
x = np.array([1, 2, 3])
why does the shape of x gives (3,) and not (1,3)?
Is it my mistake to understand the shape as (row, column)?
Because np.array([1,2,3]) is one-dimensional array. (3,) means that this is single dimension with three elements.
(1,3) means that this is a two-dimensional array.
If you use reshape() method on the array, and give it arguments (1,3), additional brackets will be added to it.
>>> np.array([1,2,3]).reshape(1,3)
array([[1, 2, 3]])
As per the docs, np.array's are multidimensional containers. Consider:
np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]).shape
# (2, 2, 2)
Also, np.shape always returns a tuple. In your example, (3,) is the representation of a one-element tuple, which is the correct value for the shape of a one-dimensional array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With