# NumPy 陣列組合與拆解

``````>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([4, 5, 6])
>>> np.concatenate([a, b])
array([1, 2, 3, 4, 5, 6])
>>>
``````

`concatenate` 可以用來串接 NumPy 陣列，要被串接的陣列得放在一個清單裡頭，如果是多維陣列，預設是將軸 0 方向上的每個元素串接起來，可以透過 `axis` 來指定軸：

``````>>> c = np.array([[10, 20, 30], [40, 50, 60]])
>>> d = np.array([[100, 200, 300], [400, 500, 600]])
>>> np.concatenate([c, d])
array([[ 10,  20,  30],
[ 40,  50,  60],
[100, 200, 300],
[400, 500, 600]])
>>> np.concatenate([c, d], axis = 0)
array([[ 10,  20,  30],
[ 40,  50,  60],
[100, 200, 300],
[400, 500, 600]])
>>> np.concatenate([c, d], axis = 1)
array([[ 10,  20,  30, 100, 200, 300],
[ 40,  50,  60, 400, 500, 600]])
>>>
``````

``````>>> a = np.array([1, 2, 3])
>>> b = np.array([4, 5, 6])
>>> np.vstack([a, b])
array([[1, 2, 3],
[4, 5, 6]])
>>> c = np.array([
...     [7, 8, 9],
...     [10, 11, 12]
... ])
>>> np.vstack([a, b, c])
array([[ 1,  2,  3],
[ 4,  5,  6],
[ 7,  8,  9],
[10, 11, 12]])
>>>
``````

`vstack` 用來對陣列進行垂直堆疊，更精確的說法是，在軸 0 的方向進行堆疊；也可以使用 `hstack` 針對軸 1 的方向，也就是水平堆疊：

``````>>> c = np.array([
...     [1, 2, 3],
...     [4, 5, 6]
... ])
>>> d = np.array([
...     [7, 8, 9],
...     [10, 11, 12]
... ])
>>> c = np.array([
...     [1, 2, 3],
...     [4, 5, 6]
... ])
>>> d = np.array([
...     [8],
...     [9]
... ])
>>> np.hstack([c, d])
array([[1, 2, 3, 8],
[4, 5, 6, 9]])
>>>
``````

``````>>> x = np.array([10, 20, 30])
>>> y = np.array([40, 50, 60])
>>> np.dstack([x, y])
array([[[10, 40],
[20, 50],
[30, 60]]])
>>>
``````

``````>>> coord = np.dstack([x, y])[0]
>>> coord
array([[10, 40],
[20, 50],
[30, 60]])
>>>
``````

``````>>> x0 = np.array([1, 2, 3])
>>> x1 = np.array([10, 20, 30])
>>> x2 = np.array([100, 200, 300])
>>> x3 = np.array([1000, 2000, 3000])
>>> v = np.dstack([x0, x1, x2, x3])
>>> v
array([[[   1,   10,  100, 1000],
[   2,   20,  200, 2000],
[   3,   30,  300, 3000]]])
>>> v[0]
array([[   1,   10,  100, 1000],
[   2,   20,  200, 2000],
[   3,   30,  300, 3000]])
>>>
``````

``````>>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a1, a2 = np.split(a, [3])
>>> a1, a2
(array([1, 2, 3]), array([4, 5, 6, 7, 8, 9]))
>>> a1, a2, a3 = np.split(a, [3, 8])
>>> a1, a2, a3
(array([1, 2, 3]), array([4, 5, 6, 7, 8]), array([9]))
>>>
``````

`split` 也可以指定 `axis`

``````>>> a = np.array([[1, 2, 3, 4], [6, 7, 8, 9]])
>>> a1, a2 = np.split(a, [2], axis = 1)
>>> a1
array([[1, 2],
[6, 7]])
>>> a2
array([[3, 4],
[8, 9]])
>>>
``````

``````>>> a = np.arange(16).reshape((4, 4))
>>> a
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15]])
>>> a1, a2 = np.split(a, [2])
>>> a1
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> a2
array([[ 8,  9, 10, 11],
[12, 13, 14, 15]])
>>>
``````