Numpy基础之线性代数
作者:互联网
线性代数
Numpy 定义了 matrix 类型,使用该 matrix 类型创建的是矩阵对象,它们的加减乘除运算缺省采用矩阵方式计算,因此用法和Matlab十分类似。但是由于 NumPy 中同时存在 ndarray 和 matrix 对象,因此用户很容易将两者弄混。这有违 Python 的“显式优于隐式”的原则,因此官方并不推荐在程序中使用 matrix。在这里,我们仍然用 ndarray 来介绍。
矩阵和向量积
矩阵的定义、矩阵的加法、矩阵的数乘、矩阵的转置与二维数组完全一致,不再进行说明,但矩阵的乘法有不同的表示。
numpy.dot(a, b[, out])计算两个矩阵的乘积,如果是一维数组则是它们的内积。
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]]
矩阵特征值与特征向量
numpy.linalg.eig(a) 计算方阵的特征值和特征向量。
numpy.linalg.eigvals(a) 计算方阵的特征值。
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
矩阵分解
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.]]
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
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.]]
范数和其它数字
矩阵的范数
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
矩阵的迹
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
标签:linalg,矩阵,基础,print,线性代数,numpy,np,array,Numpy 来源: https://blog.csdn.net/weixin_45587650/article/details/110385015