所有concatenate 系列函数都会遍历参数,无论是列表、元组还是数组
In [318]: x=np.arange(12).reshape(2,2,3)
In [319]: x
Out[319]:
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
这些都是等价的:
In [320]: np.hstack([x[0],x[1]])
Out[320]:
array([[ 0, 1, 2, 6, 7, 8],
[ 3, 4, 5, 9, 10, 11]])
In [321]: np.hstack(x)
Out[321]:
array([[ 0, 1, 2, 6, 7, 8],
[ 3, 4, 5, 9, 10, 11]])
In [322]: np.concatenate([x1 for x1 in x],axis=1)
Out[322]:
array([[ 0, 1, 2, 6, 7, 8],
[ 3, 4, 5, 9, 10, 11]])
In [323]: np.concatenate(x,axis=1)
Out[323]:
array([[ 0, 1, 2, 6, 7, 8],
[ 3, 4, 5, 9, 10, 11]])
Reshape 可以生成正确形状的数组,但顺序错误:
In [332]: x.reshape(2,6)
Out[332]:
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]])
但是如果我们先交换第一个 2 轴,那么 reshape 就可以了:
In [333]: x.transpose(1,0,2).reshape(2,6)
Out[333]:
array([[ 0, 1, 2, 6, 7, 8],
[ 3, 4, 5, 9, 10, 11]])