scipy ---- Python Scientific Computation Library
文章目录
线性代数 scipy.linalg
线性方程组
Ax = b
linalg.solve(A, b)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17import numpy as np from scipy import linalg # x + y = 2 # x - y = 4 # 解: x= 3, y = -1 In [100]: A = np.array([[1, 1], ...: [1, -1]]) In [101]: b = np.array([[2], ...: [4]]) ...: In [110]: linalg.solve(A,b) Out[110]: array([[ 3.], [-1.]])
Ax = 0
- linalg.nullspace(A)
Ax = b
使用 linalg.nullspace([A, -b])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40import numpy as np from scipy import linalg # x + y = 2 # x - y = 4 # 解: x= 3, y = -1 In [100]: A = np.array([[1, 1], ...: [1, -1]]) In [101]: b = np.array([[2], ...: [4]]) ...: # 构建增广矩阵 In [103]: M = np.concatenate([A, -b], axis=1); M Out[103]: array([[ 1, 1, -2], [ 1, -1, -4]]) # 求解零向量空间 In [106]: root = linalg.null_space(M); root Out[106]: array([[ 0.90453403], [-0.30151134], [ 0.30151134]]) # 换算求整 In [107]: root = root/np.min(np.abs(root)); root Out[107]: array([[ 3.], [-1.], [ 1.]]) # 调整参数 b 的系数成为 1 In [109]: root = root/root[-1][0]; root Out[109]: array([[ 3.], [-1.], [ 1.]])使用场景
- 方程组
- 化学反应方程求解
零空间 linalg.nullspace()
与 linalg.solve 相比,可以解参数矩阵 A 不是方阵的方程组
| |
特征值 linalg.eigenvals()
点乘 np.dot(A, B)
空间算法 scipy.spatial
功能:
- 凸包 ConvexHull
二叉树
KDTree
- k-dimensional tree
KDTree
optimize 模块
minimize
求给定函数的最小值
参考:
Scientific Python: Using SciPy for Optimization – Real Python
- 一个 tutorial
Scientific Python: Using SciPy for Optimization – Real Python
- 官方 reference
作用:
可以使用 jac 参数指定 gradient 计算函数
- 梯度下降法
文章作者
上次更新 2024-07-16 (7f33ae8)