# Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

stackoverflow上也有类似的讨论，在这里numpy vstack vs. column_stack

stack()    Join a sequence of arrays along a new axis.

hstack()    Stack arrays in sequence horizontally (column wise).

dstack()    Stack arrays in sequence depth wise (along third dimension).

concatenate()     Join a sequence of arrays along an existing axis.

vsplit ()   Split array into a list of multiple sub-arrays vertically.

>>> arrays = [np.random.randn(3, 4) for _ in range(10)]
>>> np.stack(arrays, axis=0).shape
(10, 3, 4)

>>>

>>> np.stack(arrays, axis=1).shape
(3, 10, 4)

>>>

>>> np.stack(arrays, axis=2).shape
(3, 4, 10)

>>>

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.stack((a, b))
array([[1, 2, 3],
[2, 3, 4]])

>>>

>>> np.stack((a, b), axis=-1)
array([[1, 2],
[2, 3],
[3, 4]])



>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.hstack((a,b))
array([1, 2, 3, 2, 3, 4])
>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.hstack((a,b))
array([[1, 2],
[2, 3],
[3, 4]])

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.vstack((a,b))
array([[1, 2, 3],
[2, 3, 4]])

>>>

>>> a = np.array([[1], [2], [3]])
>>> b = np.array([[2], [3], [4]])
>>> np.vstack((a,b))
array([[1],
[2],
[3],
[2],
[3],
[4]])



>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.dstack((a,b))
array([[[1, 2],
[2, 3],
[3, 4]]])

>>>

>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.dstack((a,b))
array([[[1, 2]],
[[2, 3]],
[[3, 4]]])



>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])

>>>

>>> a = np.ma.arange(3)
>>> b = np.arange(2, 5)
>>> a
fill_value = 999999)
>>> b
array([2, 3, 4])
>>> np.concatenate([a, b])
masked_array(data = [0 1 2 2 3 4],
fill_value = 999999)
>>> np.ma.concatenate([a, b])
masked_array(data = [0 -- 2 2 3 4],
mask = [False  True False False False False],
fill_value = 999999)



>>> x = np.arange(16.0).reshape(4, 4)
>>> x
array([[  0.,   1.,   2.,   3.],
[  4.,   5.,   6.,   7.],
[  8.,   9.,  10.,  11.],
[ 12.,  13.,  14.,  15.]])
>>> np.vsplit(x, 2)
[array([[ 0.,  1.,  2.,  3.],
[ 4.,  5.,  6.,  7.]]),
array([[  8.,   9.,  10.,  11.],
[ 12.,  13.,  14.,  15.]])]
>>> np.vsplit(x, np.array([3, 6]))
[array([[  0.,   1.,   2.,   3.],
[  4.,   5.,   6.,   7.],
[  8.,   9.,  10.,  11.]]),
array([[ 12.,  13.,  14.,  15.]]),
array([], dtype=float64)]

With a higher dimensional array the split is still along the first axis.
>>>

>>> x = np.arange(8.0).reshape(2, 2, 2)
>>> x
array([[[ 0.,  1.],
[ 2.,  3.]],
[[ 4.,  5.],
[ 6.,  7.]]])
>>> np.vsplit(x, 2)
[array([[[ 0.,  1.],
[ 2.,  3.]]]),
array([[[ 4.,  5.],
[ 6.,  7.]]])]



06-17 23万+
08-06 2529
01-29 58
01-14 1758
12-12 3474
11-18 6580
09-26 22
03-02 2216
09-24 213
01-05 3854