SciPy Linalg


SciPy 是使用优化的 阿特拉斯·拉帕克 and BLAS 图书馆。它具有非常快速的线性代数能力。所有这些线性代数例程都需要一个可以转换为二维数组的对象。这些例程的输出也是一个二维数组。

SciPy.linalg 与 NumPy.linalg

scipy.linalg 包含 numpy.linalg 中的所有函数。此外,scipy.linalg 还具有其他一些 numpy.linalg 中没有的高级功能。与 numpy.linalg 相比,使用 scipy.linalg 的另一个优点是它始终使用 BLAS/LAPACK 支持进行编译,而对于 NumPy,这是可选的。因此,根据 NumPy 的安装方式,SciPy 版本可能会更快。

线性方程组


The scipy.linalg.solve 特征求解线性方程 a * x + b * y = Z,对于未知的 x,y 值。

例如,假设需要求解以下联立方程。

x + 3y + 5z = 10

2x + 5y + z = 8

2x + 3y + 8z = 3

为了求解上述方程的 x、y、z 值,我们可以使用矩阵求逆来找到解向量,如下所示。

$$\begin{bmatrix} x\\ y\\ z \end{bmatrix} = \begin{bmatrix} 1 & 3 & 5\\ 2 & 5 & 1\\ 2 & 3 & 8 \end{bmatrix}^{-1} \begin{bmatrix} 10\\ 8\\ 3 \end{bmatrix} = \frac{1}{25} \begin{bmatrix} -232\\ 129\\ 19 \end{bmatrix} = \begin{bmatrix} -9.28\\ 5.16\\ 0.76 \end{bmatrix}.$$

但是,最好使用 linalg.solve 命令,它可以更快且在数值上更稳定。

求解函数接受两个输入“a”和“b”,其中“a”代表系数,“b”代表各自右侧的值,并返回解数组。

让我们考虑下面的例子。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy arrays
a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])

#Passing the values to the solve function
x = linalg.solve(a, b)

#printing the result array
print x

上述程序将生成以下输出。

array([ 2., -2., 9.])

寻找行列式


方阵 A 的行列式通常表示为 |A|是线性代数中常用的一个量。在 SciPy 中,这是使用 det() 功能。它接受一个矩阵作为输入并返回一个标量值。

让我们考虑下面的例子。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy array
A = np.array([[1,2],[3,4]])

#Passing the values to the det function
x = linalg.det(A)

#printing the result
print x

上述程序将生成以下输出。

-2.0

特征值和特征向量


特征值-特征向量问题是最常用的线性代数运算之一。通过考虑以下关系,我们可以找到方阵(A)的特征值(λ)和对应的特征向量(v):

Av = λv

scipy.linalg.eig 根据普通或广义特征值问题计算特征值.此函数返回特征值和特征向量。

让我们考虑下面的例子。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy array
A = np.array([[1,2],[3,4]])

#Passing the values to the eig function
l, v = linalg.eig(A)

#printing the result for eigen values
print l

#printing the result for eigen vectors
print v

上述程序将生成以下输出。

array([-0.37228132+0.j, 5.37228132+0.j]) #--Eigen Values
array([[-0.82456484, -0.41597356], #--Eigen Vectors
       [ 0.56576746, -0.90937671]])

奇异值分解


奇异值分解 (SVD) 可以被认为是特征值问题对非正方形矩阵的扩展。

The scipy.linalg.svd 将矩阵“a”分解为两个酉矩阵“U”和“Vh”以及奇异值(实数,非负)的一维数组“s”,使得 a == U*S*Vh,其中 'S ' 是具有主对角线 's' 的适当形状的零矩阵。

让我们考虑下面的例子。

#importing the scipy and numpy packages
from scipy import linalg
import numpy as np

#Declaring the numpy array
a = np.random.randn(3, 2) + 1.j*np.random.randn(3, 2)

#Passing the values to the eig function
U, s, Vh = linalg.svd(a)

# printing the result
print U, Vh, s

上述程序将生成以下输出。

(
    array([
        [ 0.54828424-0.23329795j, -0.38465728+0.01566714j,
        -0.18764355+0.67936712j],
        [-0.27123194-0.5327436j , -0.57080163-0.00266155j,
        -0.39868941-0.39729416j],
        [ 0.34443818+0.4110186j , -0.47972716+0.54390586j,
        0.25028608-0.35186815j]
    ]),

    array([ 3.25745379, 1.16150607]),

    array([
        [-0.35312444+0.j , 0.32400401+0.87768134j],
        [-0.93557636+0.j , -0.12229224-0.33127251j]
    ])
)