np.hstack(tup)
tup: The arrays must have the same shape along all but the second axis, except 1-D arrays which can be any length.
tup: 二维数组需要第一个维度相同;但是一维数组可以是任意长度
这是一个连接函数,将多个数组沿着水平方向连接(即对第2个维度进行连接)
二维数组连接的代码如下:
>>> a = np.array([[1],[2], [3]])
>>> b = np.array([[1,11],[2,22], [3,33]])
>>> a.shape
(3, 1)
>>> b.shape
(3, 2)
>>> np.hstack((a,b))
array([[ 1, 1, 11],
[ 2, 2, 22],
[ 3, 3, 33]])
一维数组连接的代码如下:
>>> np.hstack(([1,2], [3,4,5,6,7,8]))
array([1, 2, 3, 4, 5, 6, 7, 8])
注意:
不能将二维数组和一维数组进行连接,必须保证他们具有的维度个数相同;
多个数组应用元组()表示;
示例如下: