1. 导入 NumPy
import numpy as np
2. 创建数组
2.1 一维数组
a = np.array([1, 2, 3, 4, 5])
print(a)
2.2 多维数组
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
2.3 特殊数组
-
全零数组:
zeros = np.zeros((3, 3)) print(zeros)
-
全一数组:
ones = np.ones((3, 3)) print(ones)
-
单位矩阵:
identity = np.eye(3) print(identity)
-
随机数组:
random_array = np.random.rand(3, 3) print(random_array)
3. 数组属性
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape) # (2, 3)
print(a.dtype) # 数据类型
print(a.size) # 元素总数
print(a.ndim) # 维度数
4. 数组索引和切片
4.1 一维数组
a = np.array([1, 2, 3, 4, 5])
print(a[0]) # 1
print(a[1:4]) # [2, 3, 4]
4.2 多维数组
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(b[0, 0]) # 1
print(b[0, :]) # [1, 2, 3]
print(b[:, 1]) # [2, 5, 8]
print(b[1:3, 1:3]) # [[5, 6], [8, 9]]
5. 数组操作
5.1 数学运算
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5, 7, 9]
print(a - b) # [-3, -3, -3]
print(a * b) # [4, 10, 18]
print(a / b) # [0.25, 0.4, 0.5]
print(np.sqrt(a)) # [1., 1.41421356, 1.73205081]
5.2 广播
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([1, 0, 1])
print(a + b) # [[2, 2, 4], [5, 5, 7]]
6. 数组重塑
a = np.arange(12)
print(a) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
b = a.reshape((3, 4))
print(b) # [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
c = a.reshape((2, 2, 3))
print(c) # [[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
7. 数组连接和拆分
7.1 连接
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
# 水平连接
c = np.hstack((a, b))
print(c) # [[1, 2, 5, 6], [3, 4, 5, 6]]
# 垂直连接
d = np.vstack((a, b))
print(d) # [[1, 2], [3, 4], [5, 6]]
7.2 拆分
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
# 水平拆分
b, c = np.hsplit(a, 2)
print(b) # [[1, 2], [5, 6]]
print(c) # [[3, 4], [7, 8]]
# 垂直拆分
d, e = np.vsplit(a, 2)
print(d) # [[1, 2, 3, 4]]
print(e) # [[5, 6, 7, 8]]
8. 数组排序
a = np.array([3, 1, 2])
print(np.sort(a)) # [1, 2, 3]
b = np.array([[3, 1, 2], [6, 4, 5]])
print(np.sort(b, axis=0)) # [[3, 1, 2], [6, 4, 5]]
print(np.sort(b, axis=1)) # [[1, 2, 3], [4, 5, 6]]
9. 数组统计
a = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sum(a)) # 21
print(np.mean(a)) # 3.5
print(np.median(a)) # 3.5
print(np.min(a)) # 1
print(np.max(a)) # 6
print(np.std(a)) # 标准差
print(np.var(a)) # 方差
10. 数组布尔操作
a = np.array([1, 2, 3, 4, 5])
b = np.array([0, 1, 2, 3, 4])
print(a > 3) # [False, False, False, True, True]
print(np.any(a > 3)) # True
print(np.all(a > 3)) # False
11. 数组搜索和选择
a = np.array([1, 2, 3, 4, 5])
# 查找非零元素的索引
print(np.nonzero(a)) # (array([0, 1, 2, 3, 4]),)
# 条件选择
b = np.where(a > 3, a, 0)
print(b) # [0, 0, 0, 4, 5]
12. 文件读写
# 保存数组
np.save('array.npy', a)
# 读取数组
b = np.load('array.npy')
print(b) # [1, 2, 3, 4, 5]
13. 高级功能
13.1 广播机制
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([1, 0, 1])
print(a + b) # [[2, 2, 4], [5, 5, 7]]
13.2 线性代数
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# 矩阵乘法
print(np.dot(a, b)) # [[19, 22], [43, 50]]
# 求逆矩阵
print(np.linalg.inv(a)) # [[-2. , 1. ], [ 1.5, -0.5]]
14. 常用函数
-
生成等差数列:
a = np.arange(0, 10, 2) print(a) # [0, 2, 4, 6, 8]
-
生成等比数列:
a = np.linspace(0, 1, 5) print(a) # [0. , 0.25, 0.5 , 0.75, 1. ]
-
生成对数等比数列:
a = np.logspace(0, 1, 5) print(a) # [1. , 1.77827941, 3.16227766, 5.62341325, 10. ]