shape
shape属性指的是数组的形状
import numpy as np
data1 = np.random.randint(0,10,(2,3))
data2 = np.random.randint(0,10,(2,3,3))
print(data1.shape)
print(data2.shape)
# (2, 3)
# (2, 3, 3)
ndim
ndarray属性主要指的是数组的维度
import numpy as np
data1 = np.random.randint(0,10,(2,3))
data2 = np.random.randint(0,10,(2,3,3))
print(data1.ndim)
print(data2.ndim)
# 2
# 3
可以看到上述两个数组的shape分别是2,3和2,3,3,所以维度分别对应2维和3维
size
size属性用于计算数组中的元素个数
import numpy as np
data1 = np.random.randint(0,10,(2,3))
data2 = np.random.randint(0,10,(2,3,3))
print(data1.size)
print(data2.size)
# 6
# 18
两个数组的size分别是2*3=6以及2*3*3=18
dtype/itemsize
dtype用于确定数组中单元素类型,itemsize属性用于计算数组中单元素占用的字节数
import numpy as np
data1 = np.random.randint(0,10,(2,3),dtype=np.int32)
data2 = np.random.randn(2,3,3)
print(data1.dtype,data1.itemsize
)
print(data2.dtype,data2.itemsize)
# int32 4
# float64 8