numpy.shape gives inconsistent responses - why?

18,676

Solution 1

When you invoke the .shape attribute of a ndarray, you get a tuple with as many elements as dimensions of your array. The length, ie, the number of rows, is the first dimension (shape[0])

  • You start with an array : c=np.array([1,2]). That's a plain 1D array, so its shape will be a 1-element tuple, and shape[0] is the number of elements, so c.shape = (2,)
  • Consider c=np.array([[1,2]]). That's a 2D array, with 1 row. The first and only row is [1,2], that gives us two columns. Therefore, c.shape=(1,2) and len(c)=1
  • Consider c=np.array([[1,],[2,]]). Another 2D array, with 2 rows, 1 column: c.shape=(2,1) and len(c)=2.
  • Consider d=np.array([[1,],[2,]]).transpose(): this array is the same as np.array([[1,2]]), therefore its shape is (1,2).

Another useful attribute is .size: that's the number of elements across all dimensions, and you have for an array c c.size = np.product(c.shape).

More information on the shape in the documentation.

Solution 2

len(c.shape) is the "depth" of the array.

For c, the array is just a list (a vector), the depth is 1.
For d, the array is a list of lists, the depth is 2.

Note:

c.transpose()
# array([1, 2])

which is not d, so this behaviour is not inconsistent.

dt = d.transpose()
# array([[1],
#        [2]])
dt.shape # (2,1)

Solution 3

Quick Fix: check the .ndim property - if its 2, then the .shape property will work as you expect.

Reason Why: if the .ndim property is 2, then numpy reports a shape value that agrees with the convention. If the .ndim property is 1, then numpy just reports shape in a different way.

More talking: When you pass np.array a lists of lists, the .shape property will agree with standard notions of the dimensions of a matrix: (rows, columns).

If you pass np.array just a list, then numpy doesn't think it has a matrix on its hands, and reports the shape in a different way.

The question is: does numpy think it has a matrix, or does it think it has something else on its hands.

Share:
18,676

Related videos on Youtube

APR
Author by

APR

Updated on September 14, 2022

Comments

  • APR
    APR over 1 year

    Why does the program

    import numpy as np
    
    c = np.array([1,2])
    print(c.shape)
    d = np.array([[1],[2]]).transpose()
    print(d.shape)
    

    give

    (2,)
    (1,2)
    

    as its output? Shouldn't it be

    (1,2)
    (1,2)
    

    instead? I got this in both python 2.7.3 and python 3.2.3

    • hakre
      hakre over 11 years
      Others might ask the other way round, so I think you should tell why you expect the later.
    • seberg
      seberg over 11 years
      You may be thinking of matlab, but check the difference between array and matrix, in numpy, arrays are preferable.
    • APR
      APR over 11 years
      @hakre I don't really see any difference (in real life) between a (horizontal) list and a 1 x n matrix, so I expected the shape of the plain array to be 1 x n - and I also expected d = [1, 2] and not [[1, 2]], but this has it's own sort of logic once you see what's going on.
  • Monica Heddneck
    Monica Heddneck almost 7 years
    I don't really have a useful comment, but I feel the need to say...this kinda bugs me for some reason...