English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
NumPy 线性代数的实例
函数 | 描述 |
dot | 两个数组的点积,即元素对应相乘。 |
vdot | 两个向量的点积 |
inner | 两个数组的内积 |
matmul | 两个数组的矩阵积 |
determinant | 数组的行列式 |
solve | 求解线性矩阵方程 |
inv | 计算矩阵的乘法逆矩阵 |
numpy.dot() 对于两个一维的数组,计算的是这两个数组对应下标元素的乘积和(数学上称之为内积);对于二维数组,计算的是两个数组的矩阵乘积;对于多维数组,它的通用计算公式如下,即结果数组中的每个元素都是:数组a的最后一维上的所有元素与数组b的倒数第二位上的所有元素的乘积和: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])。
numpy.dot(a, b, out=None)
参数说明:
a : ndarray 数组 b : ndarray 数组 out : ndarray, 可选,用来保存dot()的计算结果
import numpy.matlib import numpy as np a = np.array([[1,2],[3,4]) b = np.array([[11,12],[13,14]) print(np.dot(a,b))
The output result is:
[[37 40] [85 92]]
计算式为:
[[1*11+2*13, 1*12+2*14],[3*11+4*13, 3*12+4*14]]
numpy.vdot() 函数是两个向量的点积。 如果第一个参数是复数,那么它的共轭复数会用于计算。 如果参数是多维数组,它会被展开。
import numpy.matlib import numpy as np a = np.array([[1,2],[3,4]) b = np.array([[11,12],[13,14]) # vdot 将数组展开计算内积 print (np.vdot(a,b))
The output result is:
130
计算式为:
1*11 + 2*12 + 3*13 + 4*14 = 130
numpy.inner() 函数返回一维数组的向量内积。对于更高的维度,它返回最后一个轴上的和的乘积。
import numpy.matlib print (np.inner(np.array([1,2,31,0]]) # Equivalent to 1*0+2*1+3*0
The output result is:
2
import numpy as np a = np.array([[1,2], [3,4]) print('Array a: ') print(a) b = np.array([[11, 12], [13, 14]) print ('Array b:') print(b) print ('Inner product:') print (np.inner(a,b))
The output result is:
Array a: [[1 2] [3 4]] Array b: [[11 12] [13 14]] Inner product: [[35 41] [81 95]] Array a: [[1 2] [3 4]] Array b: [[11 12] [13 14]] Inner product: [[35 41] [81 95]]
The inner product calculation formula is:
1*11+2*12, 1*13+2*14 3*11+4*12, 3*13+4*14
The numpy.matmul function returns the matrix product of two arrays. Although it returns the normal product of two-dimensional arrays, if the dimension of either parameter is greater than2, it is regarded as existing in the stack of matrices at the last two indices, and the corresponding broadcasting is performed.
On the other hand, if either parameter is a one-dimensional array, then by adding 1 To elevate it to a matrix and be removed after multiplication.
For two-dimensional arrays, it is matrix multiplication:
import numpy.matlib import numpy as np a = [[1,0],[0,1]] b = [[4,1],[2,2]] print (np.matmul(a,b))
The output result is:
[[4 1] [2 2]]
Two-dimensional and one-dimensional operations:
import numpy.matlib import numpy as np a = [[1,0],[0,1]] b = [1,2] print (np.matmul(a,b)) print (np.matmul(b,a))
The output result is:
[1 2] [1 2]
Dimension greater than two array :
import numpy.matlib import numpy as np a = np.arange(8).reshape(2,2,2) b = np.arange(4).reshape(2,2) print (np.matmul(a,b))
The output result is:
[[[ 2 3] [ 6 11]] [[10 19] [14 27]]
The numpy.linalg.det() function calculates the determinant of the input matrix.
The determinant is a very useful value in linear algebra. It is calculated from the diagonal elements of the square matrix. For 2×2 Matrix, which is the product of the upper left and lower right elements minus the product of the other two.
In other words, for the matrix [[a, b], [c, d]], the determinant is calculated as ad-bc. Larger square matrices are considered 2×2 Combination of matrices.
import numpy as np a = np.array([[1,2], [3,4]) print (np.linalg.det(a))
The output result is:
-2.0
import numpy as np b = np.array([[6,1,1], [4, -2, 5], [2,8,7]) print(b) print (np.linalg.det(b)) print (6*(-2*7 - 5*8) - 1*(4*7 - 5*2) + 1*(4*8 - -2*2))
The output result is:
[[ 6 1 1] [ 4 -2 5] [ 2 8 7]] -306.0 -306
The numpy.linalg.solve() function provides the solution to the linear equation in matrix form.
Consider the following linear equation:
x + y + z = 6 2y + 5z = -4 2x + 5y - z = 27
It can be represented by a matrix as:
If the matrix becomes A, X, and B, the equation becomes:
AX = B or X = A^(-1)B
The numpy.linalg.inv() function calculates the inverse matrix of a matrix.
Inverse matrix (inverse matrix): Let A be an n-order matrix over a field. If there exists another n-order matrix B in the same field such that: AB = BA = E, then we call B the inverse matrix of A, and A is called an invertible matrix. Note: E is the unit matrix.
import numpy as np x = np.array([[1,2],[3,4]) y = np.linalg.inv(x) print(x) print(y) print(np.dot(x, y))
The output result is:
[[1 2] [3 4]] [[-2. 1. ] [ 1.5 -0.5]] [[1.0000000e+00 0.0000000e+00] [8.8817842e-16 1.0000000e+00]]
Now create the inverse matrix of matrix A:
import numpy as np a = np.array([[1,1,1],[0,2,5],[2,5,-1]) print('Array a: ') print(a) ainv = np.linalg.inv(a) print('Inverse of a: ') print(ainv) print('Matrix b: ') b = np.array([[6],[-4],[27]) print(b) print('Calculation: A^(-1)B:') x = np.linalg.solve(a, b) print(x) # This is the linear direction x = 5, y = 3, z = -2 solution
The output result is:
Array a: [[ 1 1 1] [ 0 2 5] [ 2 5 -1]] Inverse of a: [[ 1.28571429 -0.28571429 -0.14285714] [-0.47619048 0.14285714 0.23809524] [ 0.19047619 0.14285714 -0.0952381 ]] Matrix b: [[ 6] [-4] [27]] Calculation: A^(-1)B: [[ 5.] [ 3.] [-2.]]
x = np.dot(ainv, b)