numpy.shape gives inconsistent responses - why?

后端 未结 4 1497
执笔经年
执笔经年 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:23

    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.

提交回复
热议问题