shape函数是numpy.core.fromnumeric中的函数,它的功能是读取矩阵的长度。比如说shape[0]就是读取矩阵的第一维度的长度。
shape的输入参数可以是一个整数(表示维度),也可以是一个矩阵。
例子:
1.
Import numpy as np
np.shape(0)
-> ()
2.
np.shape([[1], [2])
(2, 1)
3.可以直接用shape快速的读取矩阵的形状,使用shape[0]读取矩阵的第一维度的长度
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
4.当某一个维度长度不一致时,读取所有维度时,则不能读取出长短不一致的维度。
a = np.array([[1,2,3],[4,5],[7]])
>>> a.shape[0]
3
>>> a.shape[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range