np.concatenate使用说明及示例
时间: 2023-07-18 12:44:14 浏览: 95
np.concatenate是numpy库中的一个函数,用于将多个数组沿指定轴连接成一个新的数组。具体使用方式可以参考以下示例:
1. 沿行方向连接两个二维数组:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.concatenate((a, b), axis=0)
print(c)
# 输出结果为:
# [[1 2]
# [3 4]
# [5 6]]
```
2. 沿列方向连接两个二维数组:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5], [6]])
c = np.concatenate((a, b), axis=1)
print(c)
# 输出结果为:
# [[1 2 5]
# [3 4 6]]
```
3. 连接三个一维数组:
```python
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5])
c = np.array([6])
d = np.concatenate((a, b, c))
print(d)
# 输出结果为:
# [1 2 3 4 5 6]
```
需要注意的是,被连接的数组在连接轴上的维度大小必须相同,否则会抛出ValueError异常。另外,连接轴的大小必须与被连接的数组的维度相同,否则会抛出AxisError异常。
阅读全文
相关推荐


















