21、np中的线性代数相关函数
矩阵乘法、分解、行列式等方阵数学。
21.1 矩阵乘法
x = np.random.randint(low=0, high=20, size=(2, 5))
y = np.random.randint(low=0, high=20, size=(5, 3))
x
Out[238]:
array([[16, 11, 1, 18, 13],
[10, 10, 15, 10, 3]])
y
Out[239]:
array([[ 2, 19, 4],
[ 9, 12, 2],
[11, 4, 18],
[ 0, 13, 8],
[ 9, 5, 3]])
x.dot(y)
Out[240]:
array([[259, 739, 287],
[302, 515, 419]])
np.dot(x, y)
Out[241]:
array([[259, 739, 287],
[302, 515, 419]])
x @ y
Out[242]:
array([[259, 739, 287],
[302, 515, 419]])
21.2 矩阵求逆
x = np.random.randint(low=0, high=20, size=(2, 5))
x
Out[238]:
array([[16, 11, 1, 18, 13],
[10, 10, 15, 10, 3]])
inv(x)
Out[247]:
array([[ 0.7695024 , -0.15757419, -0.118537 , 0.08918621, -0.11866658],
[-0.47787353, 0.17338344, 0.29366982, -0.17995983, -0.01046391],
[ 0.20548788, -0.09550343, -0.27928599, 0.13032914, 0.09974731],
[-0.28025787, 0.08500713, 0.02776338, 0.00780744, 0.01467539],
[-0.08740443, -0.0114034 , 0.07859272, 0.008099 , 0.01937281]])
21.3、 矩阵分解
qr(x)
Out[248]:
(array([[-0.28082798, -0.01748697, -0.12030894, 0.61266923, -0.728692 ],
[-0.73015274, -0.02667505, -0.4188332 , -0.53075965, -0.09507027],
[-0.28082798, 0.07646846, -0.39335955, 0.57552755, 0.65522861],
[-0.39315917, 0.70837066, 0.58128321, 0.03446086, 0.0675215 ],
[-0.39315917, -0.70096093, 0.56345595, 0.10252365, 0.16151142]]),
array([[-17.80449381, -24.26353731, -22.41007266, -18.98397132,
-12.69342461],
[ 0. , -10.64334332, -9.98298774, 10.39319067,
1.59605691],
[ 0. , 0. , 3.88955001, 0.59722035,
8.60584096],
[ 0. , 0. , 0. , -5.02332045,
9.09743196],
[ 0. , 0. , 0. , 0. ,
8.33701428]]))
numpy中常用的矩阵计算函数
待续。。。。