Files
ailearning/docs/da/040.md
2020-10-19 21:08:55 +08:00

129 lines
1.4 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 矩阵
使用 `mat` 方法将 `2` 维数组转化为矩阵:
In [1]:
```py
import numpy as np
a = np.array([[1,2,4],
[2,5,3],
[7,8,9]])
A = np.mat(a)
A
```
Out[1]:
```py
matrix([[1, 2, 4],
[2, 5, 3],
[7, 8, 9]])
```
也可以使用 **Matlab** 的语法传入一个字符串来生成矩阵:
In [2]:
```py
A = np.mat('1,2,4;2,5,3;7,8,9')
A
```
Out[2]:
```py
matrix([[1, 2, 4],
[2, 5, 3],
[7, 8, 9]])
```
利用分块创造新的矩阵:
In [3]:
```py
a = np.array([[ 1, 2],
[ 3, 4]])
b = np.array([[10,20],
[30,40]])
np.bmat('a,b;b,a')
```
Out[3]:
```py
matrix([[ 1, 2, 10, 20],
[ 3, 4, 30, 40],
[10, 20, 1, 2],
[30, 40, 3, 4]])
```
矩阵与向量的乘法:
In [4]:
```py
x = np.array([[1], [2], [3]])
x
```
Out[4]:
```py
array([[1],
[2],
[3]])
```
In [5]:
```py
A * x
```
Out[5]:
```py
matrix([[17],
[21],
[50]])
```
`A.I` 表示 `A` 矩阵的逆矩阵:
In [6]:
```py
print A * A.I
```
```py
[[ 1.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 1.00000000e+00 2.08166817e-17]
[ 2.22044605e-16 -8.32667268e-17 1.00000000e+00]]
```
矩阵指数表示矩阵连乘:
In [7]:
```py
print A ** 4
```
```py
[[ 6497 9580 9836]
[ 7138 10561 10818]
[18434 27220 27945]]
```