For np.array ([1, 2, 3]) why the form (3,) instead of (3,1)?

I noticed that for a rank 1 array with 3 elements, numpy returns (3) for the shape. I know this tuple represents the size of the array along each dimension, but why is it not (3.1)?

import numpy as np

a = np.array([1, 2, 3])  # Create a rank 1 array
print a.shape            # Prints "(3,)"

b = np.array([[1,2,3],[4,5,6]])   # Create a rank 2 array
print b.shape                     # Prints "(2, 3)"
+2
source share
2 answers

In short, this is because it is a one-dimensional array (hence, a singleton tuple of a form). Perhaps the following will help clarify the situation:

>>> np.array([1, 2, 3]).shape
(3,)
>>> np.array([[1, 2, 3]]).shape
(1, 3)
>>> np.array([[1], [2], [3]]).shape
(3, 1)

We can even go into three dimensions (and higher):

>>> np.array([[[1]], [[2]], [[3]]]).shape
(3, 1, 1)
+6
source

One may also ask: β€œWhy is the form not (3,1,1,1,1,1,1)? In any case, they are equivalent.

NumPy , , broadcasting. , 3- , 3x1x1x1x1x1.

-2

Source: https://habr.com/ru/post/1655785/


All Articles