numpy_9线性代数

线性代数

矩阵和向量积
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 4, 5, 6])
z = np.dot(x, y)
print(z)  # 70

x = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
print(x)
# [[1 2 3]
#  [3 4 5]
#  [6 7 8]]

y = np.array([[5, 4, 2], [1, 7, 9], [0, 4, 5]])
print(y)
# [[5 4 2]
#  [1 7 9]
#  [0 4 5]]

z = np.dot(x, y)
print(z)
# [[  7  30  35]
#  [ 19  60  67]
#  [ 37 105 115]]

z = np.dot(y, x)
print(z)
# [[ 29  40  51]
#  [ 76  93 110]
#  [ 42  51  60]]
70
[[1 2 3]
 [3 4 5]
 [6 7 8]]
[[5 4 2]
 [1 7 9]
 [0 4 5]]
[[  7  30  35]
 [ 19  60  67]
 [ 37 105 115]]
[[ 29  40  51]
 [ 76  93 110]
 [ 42  51  60]]
矩阵特征值和特征向量
# 求方阵的特征值特征向量
import numpy as np

# 创建一个对角矩阵!
x = np.diag((1, 2, 3))  
print(x)
# [[1 0 0]
#  [0 2 0]
#  [0 0 3]]

print(np.linalg.eigvals(x))
# [1. 2. 3.]

a, b = np.linalg.eig(x)  
# 特征值保存在a中,特征向量保存在b中
print(a)
# [1. 2. 3.]
print(b)
# [[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]

# 检验特征值与特征向量是否正确
for i in range(3): 
    if np.allclose(a[i] * b[:, i], np.dot(x, b[:, i])):
        print('Right')
    else:
        print('Error')
# Right
# Right
# Right
[[1 0 0]
 [0 2 0]
 [0 0 3]]
[1. 2. 3.]
[1. 2. 3.]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Right
Right
Right
# 判断对称阵是否为正定阵(特征值是否全部为正)
import numpy as np

A = np.arange(16).reshape(4, 4)
print(A)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]
#  [12 13 14 15]]

A = A + A.T  # 将方阵转换成对称阵
print(A)
# [[ 0  5 10 15]
#  [ 5 10 15 20]
#  [10 15 20 25]
#  [15 20 25 30]]

B = np.linalg.eigvals(A)  # 求A的特征值
print(B)
# [ 6.74165739e+01 -7.41657387e+00  1.82694656e-15 -1.72637110e-15]

# 判断是不是所有的特征值都大于0,用到了all函数,显然对称阵A不是正定的
if np.all(B > 0):
    print('Yes')
else:
    print('No')
# No
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[[ 0  5 10 15]
 [ 5 10 15 20]
 [10 15 20 25]
 [15 20 25 30]]
[ 6.74165739e+01 -7.41657387e+00  2.28055691e-15 -1.91945916e-15]
No

矩阵分解

奇异值分解
import numpy as np

A = np.array([[4, 11, 14], [8, 7, -2]])
print(A)
# [[ 4 11 14]
#  [ 8  7 -2]]

u, s, vh = np.linalg.svd(A, full_matrices=False)
print(u.shape)  # (2, 2)
print(u)
# [[-0.9486833  -0.31622777]
#  [-0.31622777  0.9486833 ]]

print(s.shape)  # (2,)
print(np.diag(s))
# [[18.97366596  0.        ]
#  [ 0.          9.48683298]]

print(vh.shape)  # (2, 3)
print(vh)
# [[-0.33333333 -0.66666667 -0.66666667]
#  [ 0.66666667  0.33333333 -0.66666667]]

a = np.dot(u, np.diag(s))
a = np.dot(a, vh)
print(a)
# [[ 4. 11. 14.]
#  [ 8.  7. -2.]]
[[ 4 11 14]
 [ 8  7 -2]]
(2, 2)
[[ 0.9486833  -0.31622777]
 [ 0.31622777  0.9486833 ]]
(2,)
[[18.97366596  0.        ]
 [ 0.          9.48683298]]
(2, 3)
[[ 0.33333333  0.66666667  0.66666667]
 [ 0.66666667  0.33333333 -0.66666667]]
[[ 4. 11. 14.]
 [ 8.  7. -2.]]
import numpy as np

A = np.array([[1, 1], [1, -2], [2, 1]])
print(A)
# [[ 1  1]
#  [ 1 -2]
#  [ 2  1]]

u, s, vh = np.linalg.svd(A, full_matrices=False)
print(u.shape)  # (3, 2)
print(u)
# [[-5.34522484e-01 -1.11022302e-16]
#  [ 2.67261242e-01 -9.48683298e-01]
#  [-8.01783726e-01 -3.16227766e-01]]

print(s.shape)  # (2,)
print(np.diag(s))
# [[2.64575131 0.        ]
#  [0.         2.23606798]]

print(vh.shape)  # (2, 2)
print(vh)
# [[-0.70710678 -0.70710678]
#  [-0.70710678  0.70710678]]

a = np.dot(u, np.diag(s))
a = np.dot(a, vh)
print(a)
# [[ 1.  1.]
#  [ 1. -2.]
#  [ 2.  1.]]
[[ 1  1]
 [ 1 -2]
 [ 2  1]]
(3, 2)
[[-5.34522484e-01 -1.11022302e-16]
 [ 2.67261242e-01 -9.48683298e-01]
 [-8.01783726e-01 -3.16227766e-01]]
(2,)
[[2.64575131 0.        ]
 [0.         2.23606798]]
(2, 2)
[[-0.70710678 -0.70710678]
 [-0.70710678  0.70710678]]
[[ 1.  1.]
 [ 1. -2.]
 [ 2.  1.]]
QR分解
import numpy as np

A = np.array([[2, -2, 3], [1, 1, 1], [1, 3, -1]])
print(A)
# [[ 2 -2  3]
#  [ 1  1  1]
#  [ 1  3 -1]]

q, r = np.linalg.qr(A)
print(q.shape)  # (3, 3)
print(q)
# [[-0.81649658  0.53452248  0.21821789]
#  [-0.40824829 -0.26726124 -0.87287156]
#  [-0.40824829 -0.80178373  0.43643578]]

print(r.shape)  # (3, 3)
print(r)
# [[-2.44948974  0.         -2.44948974]
#  [ 0.         -3.74165739  2.13808994]
#  [ 0.          0.         -0.65465367]]

print(np.dot(q, r))
# [[ 2. -2.  3.]
#  [ 1.  1.  1.]
#  [ 1.  3. -1.]]

a = np.allclose(np.dot(q.T, q), np.eye(3))
print(a)  # True
[[ 2 -2  3]
 [ 1  1  1]
 [ 1  3 -1]]
(3, 3)
[[-0.81649658  0.53452248  0.21821789]
 [-0.40824829 -0.26726124 -0.87287156]
 [-0.40824829 -0.80178373  0.43643578]]
(3, 3)
[[-2.44948974  0.         -2.44948974]
 [ 0.         -3.74165739  2.13808994]
 [ 0.          0.         -0.65465367]]
[[ 2. -2.  3.]
 [ 1.  1.  1.]
 [ 1.  3. -1.]]
True
import numpy as np

A = np.array([[1, 1], [1, -2], [2, 1]])
print(A)
# [[ 1  1]
#  [ 1 -2]
#  [ 2  1]]

q, r = np.linalg.qr(A, mode='complete')
print(q.shape)  # (3, 3)
print(q)
# [[-0.40824829  0.34503278 -0.84515425]
#  [-0.40824829 -0.89708523 -0.16903085]
#  [-0.81649658  0.27602622  0.50709255]]

print(r.shape)  # (3, 2)
print(r)
# [[-2.44948974 -0.40824829]
#  [ 0.          2.41522946]
#  [ 0.          0.        ]]

print(np.dot(q, r))
# [[ 1.  1.]
#  [ 1. -2.]
#  [ 2.  1.]]

a = np.allclose(np.dot(q, q.T), np.eye(3))
print(a)  # True
[[ 1  1]
 [ 1 -2]
 [ 2  1]]
(3, 3)
[[-0.40824829  0.34503278 -0.84515425]
 [-0.40824829 -0.89708523 -0.16903085]
 [-0.81649658  0.27602622  0.50709255]]
(3, 2)
[[-2.44948974 -0.40824829]
 [ 0.          2.41522946]
 [ 0.          0.        ]]
[[ 1.  1.]
 [ 1. -2.]
 [ 2.  1.]]
True
import numpy as np

A = np.array([[1, 1], [1, -2], [2, 1]])
print(A)
# [[ 1  1]
#  [ 1 -2]
#  [ 2  1]]

q, r = np.linalg.qr(A)
print(q.shape)  # (3, 2)
print(q)
# [[-0.40824829  0.34503278]
#  [-0.40824829 -0.89708523]
#  [-0.81649658  0.27602622]]

print(r.shape)  # (2, 2)
print(r)
# [[-2.44948974 -0.40824829]
#  [ 0.          2.41522946]]

print(np.dot(q, r))
# [[ 1.  1.]
#  [ 1. -2.]
#  [ 2.  1.]]

a = np.allclose(np.dot(q.T, q), np.eye(2))
print(a)  # True   (说明q为正交矩阵)
[[ 1  1]
 [ 1 -2]
 [ 2  1]]
(3, 2)
[[-0.40824829  0.34503278]
 [-0.40824829 -0.89708523]
 [-0.81649658  0.27602622]]
(2, 2)
[[-2.44948974 -0.40824829]
 [ 0.          2.41522946]]
[[ 1.  1.]
 [ 1. -2.]
 [ 2.  1.]]
True
Cholesky分解
import numpy as np

A = np.array([[1, 1, 1, 1], [1, 3, 3, 3],
              [1, 3, 5, 5], [1, 3, 5, 7]])
print(A)
# [[1 1 1 1]
#  [1 3 3 3]
#  [1 3 5 5]
#  [1 3 5 7]]

print(np.linalg.eigvals(A))
# [13.13707118  1.6199144   0.51978306  0.72323135]

L = np.linalg.cholesky(A)
print(L)
# [[1.         0.         0.         0.        ]
#  [1.         1.41421356 0.         0.        ]
#  [1.         1.41421356 1.41421356 0.        ]
#  [1.         1.41421356 1.41421356 1.41421356]]

print(np.dot(L, L.T))
# [[1. 1. 1. 1.]
#  [1. 3. 3. 3.]
#  [1. 3. 5. 5.]
#  [1. 3. 5. 7.]]
[[1 1 1 1]
 [1 3 3 3]
 [1 3 5 5]
 [1 3 5 7]]
[13.13707118  1.6199144   0.51978306  0.72323135]
[[1.         0.         0.         0.        ]
 [1.         1.41421356 0.         0.        ]
 [1.         1.41421356 1.41421356 0.        ]
 [1.         1.41421356 1.41421356 1.41421356]]
[[1. 1. 1. 1.]
 [1. 3. 3. 3.]
 [1. 3. 5. 5.]
 [1. 3. 5. 7.]]

范数和其他数字

矩阵的范数
# 求向量的范数
import numpy as np

x = np.array([1, 2, 3, 4])

print(np.linalg.norm(x, ord=1)) 
# 10.0
print(np.sum(np.abs(x)))  
# 10

print(np.linalg.norm(x, ord=2))  
# 5.477225575051661
print(np.sum(np.abs(x) ** 2) ** 0.5)  
# 5.477225575051661

print(np.linalg.norm(x, ord=-np.inf))  
# 1.0
print(np.min(np.abs(x)))  
# 1

print(np.linalg.norm(x, ord=np.inf))  
# 4.0
print(np.max(np.abs(x)))  
# 4
10.0
10
5.477225575051661
5.477225575051661
1.0
1
4.0
4
# 求矩阵的范数
import numpy as np

A = np.array([[1, 2, 3, 4], [2, 3, 5, 8],
              [1, 3, 5, 7], [3, 4, 7, 11]])

print(A)
# [[ 1  2  3  4]
#  [ 2  3  5  8]
#  [ 1  3  5  7]
#  [ 3  4  7 11]]

print(np.linalg.norm(A, ord=1))  # 30.0
print(np.max(np.sum(A, axis=0)))  # 30

print(np.linalg.norm(A, ord=2))  
# 20.24345358700576
print(np.max(np.linalg.svd(A, compute_uv=False)))  
# 20.24345358700576

print(np.linalg.norm(A, ord=np.inf))  # 25.0
print(np.max(np.sum(A, axis=1)))  # 25

print(np.linalg.norm(A, ord='fro'))  
# 20.273134932713294
print(np.sqrt(np.trace(np.dot(A.T, A))))  
# 20.273134932713294
[[ 1  2  3  4]
 [ 2  3  5  8]
 [ 1  3  5  7]
 [ 3  4  7 11]]
30.0
30
20.24345358700576
20.24345358700576
25.0
25
20.273134932713294
20.273134932713294
方阵的行列式
# 计算行列式
import numpy as np

x = np.array([[1, 2], [3, 4]])
print(x)
# [[1 2]
#  [3 4]]

print(np.linalg.det(x))
# -2.0000000000000004
[[1 2]
 [3 4]]
-2.0000000000000004
矩阵的秩
# 计算矩阵的秩
import numpy as np

I = np.eye(3)  # 先创建一个单位阵
print(I)
# [[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]

r = np.linalg.matrix_rank(I)
print(r)  # 3

I[1, 1] = 0  # 将该元素置为0
print(I)
# [[1. 0. 0.]
#  [0. 0. 0.]
#  [0. 0. 1.]]

r = np.linalg.matrix_rank(I)  # 此时秩变成2
print(r)  # 2
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
3
[[1. 0. 0.]
 [0. 0. 0.]
 [0. 0. 1.]]
2
矩阵的迹
# 计算矩阵的迹
import numpy as np

x = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
print(x)
# [[1 2 3]
#  [3 4 5]
#  [6 7 8]]

y = np.array([[5, 4, 2], [1, 7, 9], [0, 4, 5]])
print(y)
# [[5 4 2]
#  [1 7 9]
#  [0 4 5]]

print(np.trace(x))  # A的迹等于A.T的迹
# 13
print(np.trace(np.transpose(x)))
# 13

print(np.trace(x + y))  # 和的迹 等于 迹的和
# 30
print(np.trace(x) + np.trace(y))
# 30
[[1 2 3]
 [3 4 5]
 [6 7 8]]
[[5 4 2]
 [1 7 9]
 [0 4 5]]
13
13
30
30

解方程和逆矩阵

逆矩阵(inverse matrix)
# 计算矩阵的逆矩阵
import numpy as np

A = np.array([[1, -2, 1], [0, 2, -1], [1, 1, -2]])
print(A)
# [[ 1 -2  1]
#  [ 0  2 -1]
#  [ 1  1 -2]]

# 求A的行列式,不为零则存在逆矩阵
A_det = np.linalg.det(A)  
print(A_det)
# -2.9999999999999996

A_inverse = np.linalg.inv(A)  # 求A的逆矩阵
print(A_inverse)
# [[ 1.00000000e+00  1.00000000e+00 -1.11022302e-16]
#  [ 3.33333333e-01  1.00000000e+00 -3.33333333e-01]
#  [ 6.66666667e-01  1.00000000e+00 -6.66666667e-01]]

x = np.allclose(np.dot(A, A_inverse), np.eye(3))
print(x)  # True
x = np.allclose(np.dot(A_inverse, A), np.eye(3))
print(x)  # True

A_companion = A_inverse * A_det  # 求A的伴随矩阵
print(A_companion)
# [[-3.00000000e+00 -3.00000000e+00  3.33066907e-16]
#  [-1.00000000e+00 -3.00000000e+00  1.00000000e+00]
#  [-2.00000000e+00 -3.00000000e+00  2.00000000e+00]]
[[ 1 -2  1]
 [ 0  2 -1]
 [ 1  1 -2]]
-2.9999999999999996
[[ 1.00000000e+00  1.00000000e+00 -1.11022302e-16]
 [ 3.33333333e-01  1.00000000e+00 -3.33333333e-01]
 [ 6.66666667e-01  1.00000000e+00 -6.66666667e-01]]
True
True
[[-3.00000000e+00 -3.00000000e+00  3.33066907e-16]
 [-1.00000000e+00 -3.00000000e+00  1.00000000e+00]
 [-2.00000000e+00 -3.00000000e+00  2.00000000e+00]]
求解线性方程组
#  x + 2y +  z = 7
# 2x -  y + 3z = 7
# 3x +  y + 2z =18

import numpy as np

A = np.array([[1, 2, 1], [2, -1, 3], [3, 1, 2]])
b = np.array([7, 7, 18])
x = np.linalg.solve(A, b)
print(x)  # [ 7.  1. -2.]

x = np.linalg.inv(A).dot(b)
print(x)  # [ 7.  1. -2.]

y = np.allclose(np.dot(A, x), b)
print(y)  # True
[ 7.  1. -2.]
[ 7.  1. -2.]
True

练习

# 计算两个数组a和数组b之间的欧氏距离。
import numpy as np

a = np.array([1, 2, 3, 4, 5])
b = np.array([4, 5, 6, 7, 8])

# 方法1
d = np.sqrt(np.sum((a - b) ** 2))
print(d)  # 6.708203932499369

# 方法2
d = np.linalg.norm(a - b)
print(d)  # 6.708203932499369
6.708203932499369
6.708203932499369
# 计算矩阵的行列式和矩阵的逆
np.diag([5,5,5,5,5])
array([[5, 0, 0, 0, 0],
       [0, 5, 0, 0, 0],
       [0, 0, 5, 0, 0],
       [0, 0, 0, 5, 0],
       [0, 0, 0, 0, 5]])
result=np.diag([5,5,5,5,5])
np.linalg.det(result)  # 计算行列式
3124.999999999999
# 计算矩阵的逆
np.linalg.inv(result)
array([[ 0.2,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0.2,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0.2,  0. ,  0. ],
       [-0. , -0. , -0. ,  0.2, -0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0.2]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值