数组的拼接
拼接或连接 NumPy 中的两个数组主要由np.concatenate、np.vstack 和 np.hstack 实现,此外还有dstack(),这里我们主要来介绍np.concatenate方法。
np.concatenate()
concatenate((a1, a2, ...), axis=0, out=None)
沿现有轴加入一系列数组。
- a1,a2,...:array_like的序列
- 除尺寸外,阵列必须具有相同的形状
- axis:int,可选
- 数组将连接的轴。如果axis为None,数组在使用前是扁平的。默认值为0。
尝试在右边代码框完成以下操作:
import numpy as np
x = np.array([[1, 2, 3],[4,5,6]])
y = np.array([[11,22,33],[44,55,66]])
np.concatenate((x, y),axis=0)
运行这段代码后会输出:
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 22, 33],
[44, 55, 66]])
尝试在右边代码框完成以下操作:
对数组x,y沿着横向(轴1)拼接