In [138]: x = np.array([[1., 2., 3.], [4., 5., 6.]])
In [139]: y = np.array([[6., 23.], [-1., 7], [8., 9.]])
In [140]: x
Out[140]:
array([[1., 2., 3.],
[4., 5., 6.]])
In [141]: y
Out[141]:
array([[ 6., 23.],
[-1., 7.],
[ 8., 9.]])
In [142]: x.dot(y)
Out[142]:
array([[ 28., 64.],
[ 67., 181.]])
x.dot(y)
等价np.dot(x, y)
In [143]: np.dot(x, y)
Out[143]:
array([[ 28., 64.],
[ 67., 181.]])
特殊符号@也作为中缀操作符 用于点乘矩阵操作
In [145]: x @ np.ones(3)
Out[145]: array([ 6., 15.])
numpy.linalg
拥有一个矩阵分解的标准函数集,以及其他常用函数,例如求逆和行列式求解。
In [146]: from numpy.linalg import inv, qr
In [147]: X = np.random.randn(5, 5)
In [148]: mat = X .T .dot(X)
In [149]: inv(mat)
Out[149]:
array([[ 3.81634213, -0.33734926, -8.29257805, -19.34806895,
12.16184347],
[ -0.33734926, 0.24647885, 1.26970515, 2.94427259,
-1.97488616],
[ -8.29257805, 1.26970515, 26.08526755, 60.89076132,
-38.32595465],
[-19.34806895, 2.94427259, 60.89076132, 142.58472207,
-89.65423579],
[ 12.16184347, -1.97488616, -38.32595465, -89.65423579,
56.58562833]])
In [150]: mat.dot(inv(mat))
Out[150]:
array([[ 1.00000000e+00, 0.00000000e+00, -8.88178420e-16,
0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00,
-5.68434189e-14, 5.68434189e-14],
[ 1.42108547e-14, 0.00000000e+00, 1.00000000e+00,
-5.68434189e-14, 1.70530257e-13],
[-7.10542736e-15, 0.00000000e+00, -2.84217094e-14,
1.00000000e+00, -5.68434189e-14],
[-1.42108547e-14, 0.00000000e+00, 5.68434189e-14,
0.00000000e+00, 1.00000000e+00]])
In [151]: q,r = qr(mat)
In [152]: q
Out[152]:
array([[-0.83864776, -0.39066785, 0.3558009 , 0.07715658, 0.10724336],
[ 0.41520162, -0.71433003, 0.07867346, 0.55753694, -0.01741458],
[-0.2870064 , -0.25961934, -0.85786848, -0.00839819, -0.33795897],
[ 0.10910234, -0.22246167, 0.34657706, -0.43987118, -0.79057269],
[ 0.17320954, -0.46927586, -0.1056514 , -0.69974441, 0.4989731 ]])
In [153]: r
Out[153]:
array([[-1.03628672e+00, 5.07388454e+00, -3.59320544e+00,
3.42845808e+00, 3.40120764e+00],
[ 0.00000000e+00, -9.79075704e+00, -5.06108475e+00,
-3.59439551e+00, -9.47287516e+00],
[ 0.00000000e+00, 0.00000000e+00, -1.31905677e+01,
4.44252096e+00, -1.89722959e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
-2.88582436e+00, -4.58466452e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 8.81801825e-03]])
函数 |
描述 |
diag |
将一个方阵的对角(或非对角)元素作为一维数组返回,或者将一维数组转换成一个方阵,并且在非对角线上有零点 |
dot |
矩阵点乘 |
trace |
计算对角元素和 |
det |
计算矩阵的行列式 |
eig |
计算方阵的特征值和特征向量 |
inv |
计算矩阵的逆矩阵 |
pinv |
计算矩阵的Moore-Penrose伪逆 |
qr |
计算QR分解 |
svd |
计算奇异值分解(SVD) |
solve |
求解x的线性系统Ax = b,其中A是方阵 |
lstsq |
计算Ax = b的最小二乘解 |
网友评论