官方快速入门教程:https://numpy.org/doc/stable/user/quickstart.html
Basics
安装:
导入:
NumPy 的核心对象是 同质(同 dtype)的多维数组 ndarray,维度在 NumPy 里叫 axes(轴)。常用属性:
ndim:维度数
shape:每个维度的长度(元组)
size:元素总数
dtype:元素类型
itemsize:每个元素占用字节数
例子:
1 2 3 4 5 6 7 8
| a = np.arange(15).reshape(3, 5) print(a) print("shape:", a.shape) print("ndim:", a.ndim) print("dtype:", a.dtype) print("itemsize:", a.itemsize) print("size:", a.size) print(type(a))
|
1 2 3 4 5 6 7 8 9
| [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] shape: (3, 5) ndim: 2 dtype: int64 itemsize: 8 size: 15 <class 'numpy.ndarray'>
|
arange 生成序列,reshape 改形状。
创建数组
从 Python 列表/元组创建
1 2 3 4 5 6
| a = np.array([2, 3, 4]) b = np.array([(1.5, 2, 3), (4, 5, 6)]) c = np.array([[1, 2], [3, 4]], dtype=complex) print(a, a.dtype) print(b, b.dtype) print(c, c.dtype)
|
1 2 3 4 5 6 7
| [2 3 4] int64
[[1.5 2. 3. ] [4. 5. 6. ]] float64 [[1.+0.j 2.+0.j] [3.+0.j 4.+0.j]] complex128
|
生成占位数组
1 2 3 4 5 6
| z = np.zeros((3, 4)) o = np.ones((2, 3, 4), dtype=np.int16) e = np.empty((2, 3)) print(z) print(o.dtype, o.shape) print(e)
|
1 2 3 4 5 6 7 8
| [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] int16 (2, 3, 4)
[[0. 0. 0.] [0. 0. 0.]]
|
生成等差/等分序列
1 2 3 4
| print(np.arange(10, 30, 5)) print(np.arange(0, 2, 0.3))
print(np.linspace(0, 2, 9))
|
打印数组
大数组会自动省略中间:
1 2
| print(np.arange(10000)) print(np.arange(10000).reshape(100, 100))
|
1 2 3 4 5 6 7 8
| [ 0 1 2 ... 9997 9998 9999] [[ 0 1 2 ... 97 98 99] [ 100 101 102 ... 197 198 199] [ 200 201 202 ... 297 298 299] ... [9700 9701 9702 ... 9797 9798 9799] [9800 9801 9802 ... 9897 9898 9899] [9900 9901 9902 ... 9997 9998 9999]]
|
想强制全打印可以用 np.set_printoptions(...)。
基本运算
逐元素四则运算
1 2 3 4
| a = np.array([20, 30, 40, 50]) b = np.arange(4) print(a - b) print(b**2)
|
NumPy数组上的算术运算默认是逐元素,并且通常会生成一个新数组。
矩阵乘法
@:
1 2 3 4 5
| A = np.array([[1, 1], [0, 1]]) B = np.array([[2, 0], [3, 4]]) print(A @ B)
|
常见聚合
1 2 3 4
| m = np.arange(12).reshape(3, 4) print("sum:", m.sum()) print("sum axis=0 (按列):", m.sum(axis=0)) print("sum axis=1 (按行):", m.sum(axis=1))
|
1 2 3
| sum: 66 sum axis=0 (按列): [12 15 18 21] sum axis=1 (按行): [ 6 22 38]
|
通用函数
对数组整体无for循环的计算:
1 2 3 4
| x = np.linspace(0, 2*np.pi, 5) print(np.sin(x)) print(np.exp(x)) print(np.sqrt(x))
|
会自动对数组逐元素计算。
索引、切片、迭代
切片
1 2
| a = np.arange(10) print(a[2:7:2])
|
多维索引与切片
1 2 3 4
| b = np.arange(12).reshape(3, 4) print(b[1, 2]) print(b[:, 1]) print(b[0:2, 1:3])
|
遍历:flat 按元素展开迭代
1 2
| for element in b.flat: pass
|
形状操作(Shape manipulation)
最常用的:
reshape:改形状(不一定复制数据)
ravel:展平(通常返回视图)
T / transpose:转置/换轴
1 2 3 4 5 6
| a = np.arange(12) b = a.reshape(3, 4) print(b)
print(b.ravel()) print(b.T)
|
拼接与拆分(stacking / splitting)
1 2 3 4 5 6 7 8 9 10
| a = np.arange(6).reshape(2, 3) b = np.arange(6, 12).reshape(2, 3)
print(np.vstack([a, b])) print(np.hstack([a, b]))
m = np.arange(12).reshape(3, 4) left, right = np.hsplit(m, 2) print(left) print(right)
|
视图与拷贝(Copies and views)
1 2 3 4
| a = np.arange(10) b = a[2:7] b[:] = 99 print(a)
|
b = a[2:7]得到的是 view(视图):
a 和 b 是 两个不同的 ndarray 对象(id(a) 和 id(b) 不同)
- 但它们 共享同一块底层数据缓冲区(同一个指针,同一片内存里的数据)
1 2 3 4
| a = np.arange(10) b = a[2:7].copy() b[:] = 99 print(a)
|
b = a[2:7].copy() 得到的是 拷贝:
b 仍然是一个新的 ndarray 对象
- 且它有自己的新数据缓冲区(不共享内存)
高级索引
整数数组索引(一次取多个位置)
1 2 3
| a = np.arange(12)**2 i = np.array([1, 1, 3, 8, 5]) print(a[i])
|
布尔索引(按条件过滤/赋值)
1 2 3 4
| a = np.arange(12).reshape(3, 4) mask = a > 4 a[mask] = 0 print(a)
|