4

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)"
Alaa Awad
  • 2,212
  • 3
  • 21
  • 34

2 Answers2

9

In a nutshell, this is 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)
NPE
  • 438,426
  • 93
  • 887
  • 970
-2

You could equally ask, "Why is the shape not (3,1,1,1,1,1,1)? They are equivalent, after all.

NumPy often chooses to collapse singular dimensions, or treat them as optional, such as during broadcasting. This is powerful because a 3-vector has exactly the same number of elements, and in the same relative orientation, as a 3x1x1x1x1x1 matrix.

John Zwinck
  • 207,363
  • 31
  • 261
  • 371
  • 1
    This is incorrect - broadcasting ignores _leading_ dimensions of length 1, not trailing ones. So `1x1x1x1x1x3` is the same as `3` – Eric Sep 26 '16 at 09:15
  • `numpy` can add leading 1s during broadcasting, but it does not ignore them, whether leading or trailing. You have use `squeeze` or indexing to remove them. – hpaulj Oct 22 '16 at 06:45