它们之间的区别是什么
np.shape(X[1])和
X.shape[0]其中X是一个数组。
发布于 2021-10-31 15:55:18
np.shape是一个函数;x.shape是一个属性,通常返回相同的东西。
制作一个示例数组:
In [119]: x = np.ones((1,2,3),int)
In [120]: x
Out[120]:
array([[[1, 1, 1],
[1, 1, 1]]])
In [121]: x.shape
Out[121]: (1, 2, 3) # a tuple
In [122]: x.shape[1]
Out[122]: 2 # select the 2nd element of the tuple该函数返回相同的元组:
In [123]: np.shape(x)
Out[123]: (1, 2, 3)这里首先执行x[0],返回一个新的数组:
In [124]: np.shape(x[0])
Out[124]: (2, 3)
In [125]: x[0].shape
Out[125]: (2, 3)np.shape可以接受非数组,如列表列表
In [126]: np.shape([[1],[2]])
Out[126]: (2, 1)但是如果你想要第二维,你可以使用size,它只返回一个数字(取决于可选的轴参数):
In [127]: np.size(x,1)
Out[127]: 2
In [128]: np.size(x)
Out[128]: 6https://stackoverflow.com/questions/69786237
复制相似问题