numpy.shape gives inconsistent responses - why?

后端 未结 4 1495
执笔经年
执笔经年 2021-02-04 03:29

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

<         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 04:13

    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)
    

提交回复
热议问题