Alaa Awad - 1 year ago 114

Python Question

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)"

Answer Source

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)
```