numpy 二维数组按列合并

numpy的数组默认按行合并,如何按列合并?
 
先生产测试数组
# np 的运算
n = np.arange(1,17)

转化形态:
n0 = n.reshape(4,4)
变成4x4的矩阵
array([[ 1,  2,  3,  4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])

然后运用concatenate函数拼接:
np.concatenate((n0,n0),axis=1)
array([[ 1, 2, 3, 4, 1, 2, 3, 4], [ 5, 6, 7, 8, 5, 6, 7, 8], [ 9, 10, 11, 12, 9, 10, 11, 12], [13, 14, 15, 16, 13, 14, 15, 16]])

 这个只需要添加参数axis=1就可以了,axis默认是为0,按照行拼接。
np.concatenate((n0,n0),axis=1)

array([[ 1,  2,  3,  4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])

0 个评论

要回复文章请先登录注册