SciPy Linear Algebra

chapter


SciPy linear algebra package is used and optimized ATLAS LAPACK BLAS library construction, with efficient linear algebra operations capability.

Linear algebra package function, the operation target is a two-dimensional array.

SciPy.linalg and NumPy.linalg
compared with NumPy.linalg, scipy.linalg contains all the functions of numpy.linalg, also has advanced features numpy.linalg not found.

Linear Equations

scipy.linalg.solve function can be used for Linear Equations. For example, the linear equation for $ a * x + b * y = z $, determined unknowns x, y values.

Examples

Solving the following simultaneous equations:

$$
x + 3y + 5z = 10 \
2x + 5y + z = 8 \
2x + 3y + 8z = 3
$$

The above equations can be expressed by a matrix as:

$$
\left[
\begin{matrix}
1 & 3 & 5 \
2 & 5 & 1 \
2 & 3 & 8
\end{matrix}
\right]

\left[
\begin{matrix}
x \
y \
z
\end{matrix}
\right] =

\left[
\begin{matrix}
10 \
8 \
3
\end{matrix}
\right]

$$

Solving the above equations using the matrix, as shown below:

$$
\left[
\begin{matrix}
x \
y \
z
\end{matrix}
\right]

=

\left[
\begin{matrix}
1 & 3 & 5 \
2 & 5 & 1 \
2 & 3 & 8
\end{matrix}
\right]^{-1}

\left[
\begin{matrix}
10 \
8 \
3
\end{matrix}
\right]

= \frac{1}{25}

\left[
\begin{matrix}
-232 \
129 \
19
\end{matrix}
\right]

=

\left[
\begin{matrix}
-9.28 \
5.16 \
0.76
\end{matrix}
\right]

$$

Here we use scipy to solve.

scipy.linalg.solveFunction accepts two inputs, arrays aand array b, the array arepresents a coefficient array brepresents the value of the right side of the equal sign, the obtained solution will be placed in an array to return.

Let us consider the following example.

# 导入scipy和numpy包
from scipy import linalg
import numpy as np

# 声明numpy数组
a = np.array([[1, 3, 5], [2, 5, 1], [2, 3, 8]])
b = np.array([10, 8, 3])

# 求解
x = linalg.solve(a, b)

# 输出解值
print (x)

Export

[-9.28  5.16  0.76]

Determinant calculation

Determinant of a matrix A is represented as $ | A | $, determinant calculation operation is a common linear algebra.

SciPy may use the det()function to calculate the determinant of a matrix which accepts as input and returns a scalar value, i.e., the determinant of the matrix.

Examples

# 导入scipy和numpy包
from scipy import linalg
import numpy as np

# 声明numpy数组
A = np.array([[3,4],[7,8]])

# 计算行列式
x = linalg.det(A)

# 输出结果
print (x)

Export

-4.0

Obtaining eigenvalue and eigenvector

Obtaining eigenvalues, eigenvectors, is a common calculation of linear algebra.

Generally, according to the following relation, obtains characteristic value ([lambda]) matrix (A), the eigenvector (v):

$$ Off = λv $$

scipy.linalg.eig function is used to calculate eigenvalues ​​and eigenvectors, the function returns the eigenvalues ​​and eigenvectors.

Examples

# 导入scipy和numpy包
from scipy import linalg
import numpy as np

# 声明numpy数组
A = np.array([[3,4],[7,8]])

# 求解
l, v = linalg.eig(A)

# 打印特征值
print('特征值')
print (l)

# 打印特征向量
print('特征向量')
print (v)

The above procedure produces the following output.

特征值
[-0.35234996+0.j 11.35234996+0.j]
特征向量
[[-0.76642628 -0.43192981]
 [ 0.64233228 -0.90190722]]

SVD Singular Value Decomposition

Singular value decomposition (SVD) is now one of the more common algorithm is data mining engineers, one engineer algorithms necessary skills. Suppose A is a $ M × N $ matrix, the matrix will be decomposed by $ U, Σ, V ^ T $ (V transpose) three matrix, where U is the $ M × M $ square , it is called left singular vectors, which matrix is ​​orthogonal vectors; [Sigma is a $ M × N $ diagonal matrix, among other is 0, the value of the diagonal elements of the diagonal called Singular value; $ V ^ T $ (V transpose) is a $ N × N $ matrix, is called right singular vector, which vector matrix are also orthogonal.

$$ A_{m\times{n}} = U_{m\times{m}} Σ_{m\times{n}} V_{n\times{n}}^T$$

Let us consider the following example.

# 导入scipy和numpy包
from scipy import linalg
import numpy as np

# 声明numpy数组
a = np.random.randn(3, 2) + 1.j*np.random.randn(3, 2)

# 输出原矩阵
print('原矩阵')
print(a)

# 求解
U, s, Vh = linalg.svd(a)

# 输出结果
print('奇异值分解')
print(U, "#U")
print(Vh, "#Vh")
print(s, "#s")

The above procedure produces the following output.

原矩阵
[[ 1.81840014+0.16615057j -0.47446573-2.36327076j]
 [-0.19366846-0.44489565j -0.03227288+0.02260894j]
 [-0.91921239-0.99340761j -1.33606096+0.40858722j]]
奇异值分解
[[-0.84399035+0.03548862j -0.1574924 +0.44602345j  0.08723906-0.23466874j]
 [ 0.03893388+0.08672055j -0.19156838-0.45118633j -0.02718865-0.86600053j]
 [ 0.23121352+0.47320699j -0.71944217+0.13562682j  0.41089761+0.13336765j]] #U
[[-0.63461867+0.j          0.05670247+0.77074248j]
 [ 0.77282543+0.j          0.04656219+0.63290822j]] #Vh
[3.55734783 0.7144458 ] #s

Guess you like

Origin www.cnblogs.com/jinbuqi/p/11819411.html
Recommended