numpy.shape gives inconsistent responses - why?
Question: I'm a newbie to python. What I would like to know is, 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

Thanks!


Answer: 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](http://www.scipy.org/Tentative_NumPy_Tutorial#head-62ef2d3c0a5b4b7d6fdc48e4a60fe48b1ffe5006).

