判断numpy数组的维度

python中,可以用以下函数来判断numpy数组的维度:

  • ndarray.ndim:返回数组的维数(轴数)。
  • ndarray.shape:返回一个元组,表示每个维度的大小。
  • ndarray.size:返回数组中元素的总数。
>>> import numpy as np
>>> a=np.zeros((4,8))
>>> print(a)
[[0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]]
>>> print(np.ndim(a))
2
>>> print(np.shape(a))
(4, 8)
>>> print(np.size(a))
32

猜你喜欢

转载自blog.csdn.net/weixin_39509073/article/details/129568550
今日推荐