Numpy线性计算

Numpy内置方法以及numpy.linalg模块可实现矩阵乘法、矩阵分解、矩阵行列式等线性代数的计算。

In [1]: import numpy as np

In [2]: x = np.arange(6,dtype='float').reshape(2,3)
In [3]: x
Out[3]:
array([[0., 1., 2.],
       [3., 4., 5.]])

In [4]: y = np.arange(6,dtype='int').reshape(3,2)
In [5]: y
Out[5]:
array([[0, 1],
       [2, 3],
       [4, 5]])

#x.dot(y)和np.dot(x,y)一样可以实现矩阵点乘
In [6]: x.dot(y)
Out[6]:
array([[10., 13.],
       [28., 40.]])

In [7]: np.dot(x,y)
Out[7]:
array([[10., 13.],
       [28., 40.]])

In [8]: np.dot(x,np.ones(3))
Out[8]: array([ 3., 12.])

In [10]: np.dot(x,np.ones(3).reshape(3,1))
Out[10]:
array([[ 3.],
       [12.]])

In [11]: from numpy.linalg import inv, qr
In [12]: x = np.random.randn(5,5)

In [13]: mat = x.T.dot(x)   
#inv(arr)实现逆矩阵求解
In [14]: inv(mat)
Out[14]:
array([[ 1.84895223,  0.52000274, -1.6102196 ,  0.50430018,  0.85699129],
       [ 0.52000274,  0.79958507, -0.6711184 ,  0.20499197,  0.5296455 ],
       [-1.6102196 , -0.6711184 ,  1.94704649, -0.14636372, -1.09774765],
       [ 0.50430018,  0.20499197, -0.14636372,  0.9424341 , -0.2083091 ],
       [ 0.85699129,  0.5296455 , -1.09774765, -0.2083091 ,  1.05277181]])
#矩阵乘以逆矩阵得到单位矩阵
In [15]: mat.dot(inv(mat))
Out[15]:
array([[ 1.00000000e+00, -5.71287376e-16, -4.78160183e-16,
         1.36711210e-16, -8.70752759e-17],
       [ 1.59513968e-17,  1.00000000e+00, -1.78338097e-17,
        -1.27809360e-16,  2.59687796e-16],
       [-8.31909138e-16, -1.44352653e-16,  1.00000000e+00,
        -8.62450849e-17,  6.01921269e-16],
       [-7.90282618e-16,  2.53674571e-16,  6.26183310e-16,
         1.00000000e+00, -2.40653733e-16],
       [ 4.87373871e-16, -1.53371320e-16, -1.03317523e-15,
         1.98732722e-16,  1.00000000e+00]])
#qr(arr)实现矩阵因式分解,r为上三角矩阵,q为标准正交矩阵
In [16]: q,r = qr(mat)

In [17]: r
Out[17]:
array([[-4.77016813, -2.7746349 , -3.21063899,  3.73904397,  2.92899001],
       [ 0.        , -2.55332826,  1.1479357 ,  1.56574687,  3.17486777],
       [ 0.        ,  0.        , -2.09076931, -1.2290909 , -2.99498757],
       [ 0.        ,  0.        ,  0.        , -0.90346691,  0.07658242],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.54459499]])

In [18]: q
Out[18]:
array([[-0.69707745,  0.33427417,  0.18008871, -0.38998806,  0.46671316],
       [-0.22653787, -0.80948395, -0.43508171, -0.14464193,  0.28844228],
       [-0.47068893,  0.23429759, -0.60319018,  0.04816661, -0.59782786],
       [ 0.4092138 ,  0.12283144, -0.22844122, -0.86741083, -0.11344409],
       [ 0.27158721,  0.40374942, -0.60187057,  0.26882419,  0.57333425]])
#标准正交矩阵乘以其逆矩阵等于单位矩阵
In [19]: q.dot(inv(q))
Out[19]:
array([[ 1.00000000e+00,  8.91951189e-18,  6.81387594e-18,
         1.10666298e-17,  1.97034778e-17],
       [ 1.51199514e-17,  1.00000000e+00, -1.18531754e-17,
        -2.02130063e-17, -2.07169311e-17],
       [-1.55111623e-17,  8.77051635e-17,  1.00000000e+00,
         6.24561853e-18,  1.54457642e-16],
       [ 1.18675764e-16,  5.73913447e-17, -2.96557631e-17,
         1.00000000e+00, -1.40072379e-17],
       [ 3.59970210e-17,  8.66286674e-17,  2.17131897e-17,
        -3.14254018e-17,  1.00000000e+00]])

#求解AX = B 方程,A为方阵
In [20]: A = [[1,1,1],[0,2,5],[2,5,-1]]
In [21]: B = [6,-4,27]

In [22]: np.linalg.solve(A,B)
Out[22]: array([ 5.,  3., -2.])

numpy.linalg模块常用线性函数方法

官网内容:Linear algebra (numpy.linalg) — NumPy v1.21 Manual

函数方法 定义
diag 以一维数组的形式返回方阵对角线(或非对角线)元素,或将一维数组转换成方阵(非对角线元素为0)
dot 矩阵乘法
trace 计算对角线元素的和
det 计算矩阵行列式
eig 计算方阵的特征值和特征向量
inv 计算方阵的逆方阵
pinv 计算矩阵的Moore-Penrose伪逆
qr 计算QR分解(因式分解),r为上三角矩阵,q为标准正交矩阵
svd 计算奇异值分解(SVD)
solve 解线性方程组:Ax = b,其中 A为一个方阵
lstsq 计算Ax = b的最小乘解
上一篇:K-means聚类算法java实现


下一篇:Spring源码分析(五)推断构造方法