Alaa Awad - 1 year ago 170
Python Question

# For a np.array([1, 2, 3]) why is the shape (3,) instead of (3,1)?

I noticed that for a rank 1 array with 3 elements numpy returns (3,) for the shape. I know that this tuple represents the size of the array along each dimension, but why isn't it (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)"
``````

In a nutshell, it's because it's one-dimensional array (hence the one-element shape tuple). Perhaps the following will help clear things up:

``````>>> 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 three dimensions (and higher):

``````>>> np.array([[[1]], [[2]], [[3]]]).shape
(3, 1, 1)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download