读书笔记 numpy入门 第2章 数据属性

https://github.com/jakevdp/PythonDataScienceHandbook

NumPy数组的属性

  • ndim 数组的维度
  • shape 数组每个维度的大小
  • size 数组的总大小
  • dtype 数组的数据类型
  • itemsize 每个数组元素字节的大小
  • nbytes 数组总字节大小的属性

用例子体会

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
np.random.seed(0)

x1 = np.random.randint(10, size=6)
x2 = np.random.randint(10, size=(3, 4)) # 二维数组
x3 = np.random.randint(10, size=(3, 4, 5)) # 三维数组

print("x3 ndim: ", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)

x3 ndim: 3
x3 shape: (3, 4, 5)
x3 size: 60

print("dtype:", x3.dtype)

>>>dtype: int64

print("itemsize:", x3.itemsize, "bytes")
print("nbytes:", x3.nbytes, "bytes")

itemsize: 8 bytes
nbytes: 480 bytes