如何使用numpy库进行矩阵运算?
时间: 2024-02-21 10:54:45 浏览: 87
使用numpy库进行矩阵运算有以下几种常见的方法:
1. 创建矩阵:
```python
import numpy as np
# 创建一个2x2的矩阵
a = np.array([[1, 2], [3, 4]])
print(a)
```
2. 矩阵加法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.add(a, b)
print(c)
```
3. 矩阵减法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.subtract(a, b)
print(c)
```
4. 矩阵乘法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.dot(a, b)
print(c)
```
5. 矩阵转置:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
c = np.transpose(a)
print(c)
```
6. 矩阵求逆:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
c = np.linalg.inv(a)
print(c)
```
阅读全文