Numpy常用属性和函数（三）

• 创建矩阵

1. 直接创建

```>>> A = np.mat([[1,2],[3,4]])#列表
>>> A
matrix([[1, 2],
[3, 4]])
>>> B=np.mat("3,2,1;7,6,8")#数字字符串

>>> B
matrix([[3, 2, 1],
[7, 6, 8]])
>>> C = np.mat(((1,2),(3,4)))#元组
>>> C
matrix([[1, 2],
[3, 4]])
```

2. 从numpy数组创建矩阵

```>>> a=np.array([[1,2],[3,4]])
>>> np.mat(a)
matrix([[1, 2],
[3, 4]])```

3. 从已有矩阵中通过bmat函数创建

```>>> A = np.mat(np.eye(2))
>>> A
matrix([[1., 0.],
[0., 1.]])
>>> B = A*2
>>> B
matrix([[2., 0.],
[0., 2.]])
>>> np.bmat("A B; B A")
matrix([[1., 0., 2., 0.],
[0., 1., 0., 2.],
[2., 0., 1., 0.],
[0., 2., 0., 1.]])```
• 矩阵的加法、减法、乘法、数乘，对应元素相乘 >>> A = np.mat(np.eye(2)) >>> A matrix([[1., 0.], [0., 1.]]) >>> B = A*2#数乘 >>> B matrix([[2., 0.], [0., 2.]]) >>> A+B matrix([[3., 0.], [0., 3.]]) >>> A-B matrix([[-1., 0.], [ 0., -1.]]) >>> A*B #矩阵乘法 matrix([[2., 0.], [0., 2.]]) >>> A = np.mat([[1,2],[3,4]]) >>> A*A #矩阵乘法 matrix([[ 7, 10], [15, 22]]) >>> np.multiply(A,A) #同形矩阵对应元素相乘 matrix([[ 1, 4], [ 9, 16]])
• 矩阵的转置
```>>> A = np.mat(np.random.rand(3,2))
>>> A
matrix([[0.88548289, 0.32510849],
[0.4915165 , 0.52896972],
[0.32423831, 0.59844983]])
>>> A.T
matrix([[0.88548289, 0.4915165 , 0.32423831],
[0.32510849, 0.52896972, 0.59844983]])```
• 方阵的行列式
```>>> A = np.random.randn(3,3)
>>> A = np.mat(A)
>>> A
matrix([[ 0.62735613, -0.58155189,  0.33313612],
[-0.56945614,  1.02485754,  0.16677127],
[-0.54911992, -0.80337017,  1.68631462]])
>>> np.linalg.det(A) #需用到线性代数子库linalg
1.0029561427173685```
• 矩阵的秩
```>>> A=np.mat([[1,2,3],[2,4,6]])
>>> np.linalg.matrix_rank(A)
1
>>> B=np.mat([[1,2,3],[2,4,5]])
>>> np.linalg.matrix_rank(B)
2```

```>>> A = np.random.randn(3,3)
>>> A= np.mat(A)
>>> A
matrix([[-0.01513886,  0.90067747, -0.97224512],
[-0.49613961, -0.96700178, -1.28164602],
[-1.16368445, -0.3528249 , -1.0021803 ]])
>>> A.I #前提是det(A)不为0
matrix([[ 0.28535139,  0.68764824, -1.1562322 ],
[ 0.54883314, -0.61618317,  0.25557115],
[-0.52455728, -0.58153289,  0.25476211]])
>>> A**(-1)
matrix([[ 0.28535139,  0.68764824, -1.1562322 ],
[ 0.54883314, -0.61618317,  0.25557115],
[-0.52455728, -0.58153289,  0.25476211]])
>>> A*A.I #由于精度问题，结果近似为单位矩阵
matrix([[ 1.00000000e+00, -8.96322141e-17, -1.70539774e-18],
[-1.51263266e-16,  1.00000000e+00,  2.74501368e-17],
[-2.67373528e-17,  1.46098928e-17,  1.00000000e+00]])
```
• 求解线性方程组 A*X = B
```>>> A = np.mat("1,3;5,2")
>>>B=np.mat([[11], [16]])
>>> X=np.linalg.solve(A,B) #det(A)!=0时，X=A.I * B
>>> X
matrix([[2.],
[3.]])
>>> A*X == B #验证
matrix([[ True],
[ True]])```
• 特征值和特征向量

```>>> A = np.mat(np.random.randn(3,3))
>>> A
matrix([[ 0.42631062,  2.64734627, -2.11149817],
[-1.23947749, -0.63390069,  0.42119854],
[ 0.61590965, -0.23284082,  0.47037727]])
>>> eigenvalues,eigvector=np.linalg.eig(A)
>>> eigenvalues #特征值为数组
array([0.04862084+2.07208694j, 0.04862084-2.07208694j,
0.16554552+0.j        ])
>>> eigvector #特征向量为矩阵
matrix([[ 0.82817694+0.j        ,  0.82817694-0.j        ,
-0.1429864 +0.j        ],
[-0.19877709+0.45047014j, -0.19877709-0.45047014j,
0.62570857+0.j        ],
[-0.10108359-0.2479302j , -0.10108359+0.2479302j ,
0.76684006+0.j        ]])```
• 奇异值分解（SVD) 假设M是一个m×n阶矩阵，其中的元素全部属于域 K，也就是实数域或复数域。如此则存在一个分解使得

• 奇异值分解（SVD)原理可在机器学习中给数据降维。
```>>> A =np.mat("2 6 14;8 3 2")
>>> A
matrix([[ 2,  6, 14],
[ 8,  3,  2]])
>>> U,sigma,V=np.linalg.svd(A,full_matrices=False)
>>> U
matrix([[-0.94566125, -0.3251535 ],
[-0.3251535 ,  0.94566125]])
>>> sigma
array([16.04113167,  7.46204361])
>>> V
matrix([[-0.28006444, -0.41452362, -0.86587186],
[ 0.92668756,  0.11874264, -0.35658147]])
>>> U*np.diag(sigma)*V #验证。结果等于A(有误差）
matrix([[ 2.,  6., 14.],
[ 8.,  3.,  2.]])```

129 篇文章23 人订阅

0 条评论