shape函数是numpy.core.fromnumeric中的函数,它的功能是查看矩阵或者数组的维数。
举例说明:
- A=array([[ 1., 0., 0.],
- [ 0., 1., 0.],
- [ 0., 0., 1.]])
- >>> A.shape
- (3, 3)
- >>> b =array([1,2,3,4])
- >>> b.shape
- (4,)
- #可以简写
- >>> shape([1,2,3,4])
- (4,)
- >>>
>>> b =array([1,2,3,4]) >>> b.shape (4,) #可以简写 >>> shape([1,2,3,4]) (4,) >>>
建立一个4×2的矩阵c, c.shape[1] 为第一维的长度,c.shape[0] 为第二维的长度。
- >>> c = array([[1,1],[1,2],[1,3],[1,4]])
- >>> c.shape
- (4, 2)
- >>> c.shape[0]
- 4
- >>> c.shape[1]
- 2
>>> c = array([[1,1],[1,2],[1,3],[1,4]]) >>> c.shape (4, 2) >>> c.shape[0] 4 >>> c.shape[1] 2一个单独的数值,返回值为空
- >>> shape(3) >>> shape(3)
2.()