如果要将两份数据组合到一起,就需要拼接操作。

    • numpy.concatenate((a1, a2, ...), axis=0, out=None) Join a sequence of arrays along an existing axis.

    【例】连接沿现有轴的数组序列(原来x,y都是一维的,拼接后的结果也是一维的)。

    1. import numpy as np
    2. x = np.array([1, 2, 3])
    3. y = np.array([7, 8, 9])
    4. z = np.concatenate([x, y])
    5. print(z)
    6. # [1 2 3 7 8 9]
    7. z = np.concatenate([x, y], axis=0)
    8. print(z)
    9. # [1 2 3 7 8 9]

    【例】原来x,y都是二维的,拼接后的结果也是二维的。

    1. import numpy as np
    2. x = np.array([1, 2, 3]).reshape(1, 3)
    3. y = np.array([7, 8, 9]).reshape(1, 3)
    4. z = np.concatenate([x, y])
    5. print(z)
    6. # [[ 1 2 3]
    7. # [ 7 8 9]]
    8. z = np.concatenate([x, y], axis=0)
    9. print(z)
    10. # [[ 1 2 3]
    11. # [ 7 8 9]]
    12. z = np.concatenate([x, y], axis=1)
    13. print(z)
    14. # [[ 1 2 3 7 8 9]]

    【例】x,y在原来的维度上进行拼接。

    1. import numpy as np
    2. x = np.array([[1, 2, 3], [4, 5, 6]])
    3. y = np.array([[7, 8, 9], [10, 11, 12]])
    4. z = np.concatenate([x, y])
    5. print(z)
    6. # [[ 1 2 3]
    7. # [ 4 5 6]
    8. # [ 7 8 9]
    9. # [10 11 12]]
    10. z = np.concatenate([x, y], axis=0)
    11. print(z)
    12. # [[ 1 2 3]
    13. # [ 4 5 6]
    14. # [ 7 8 9]
    15. # [10 11 12]]
    16. z = np.concatenate([x, y], axis=1)
    17. print(z)
    18. # [[ 1 2 3 7 8 9]
    19. # [ 4 5 6 10 11 12]]
    • numpy.stack(arrays, axis=0, out=None)Join a sequence of arrays along a new axis.

    【例】沿着新的轴加入一系列数组(stack为增加维度的拼接)。

    1. import numpy as np
    2. x = np.array([1, 2, 3])
    3. y = np.array([7, 8, 9])
    4. z = np.stack([x, y])
    5. print(z.shape) # (2, 3)
    6. print(z)
    7. # [[1 2 3]
    8. # [7 8 9]]
    9. z = np.stack([x, y], axis=1)
    10. print(z.shape) # (3, 2)
    11. print(z)
    12. # [[1 7]
    13. # [2 8]
    14. # [3 9]]

    【例】

    1. import numpy as np
    2. x = np.array([1, 2, 3]).reshape(1, 3)
    3. y = np.array([7, 8, 9]).reshape(1, 3)
    4. z = np.stack([x, y])
    5. print(z.shape) # (2, 1, 3)
    6. print(z)
    7. # [[[1 2 3]]
    8. #
    9. # [[7 8 9]]]
    10. z = np.stack([x, y], axis=1)
    11. print(z.shape) # (1, 2, 3)
    12. print(z)
    13. # [[[1 2 3]
    14. # [7 8 9]]]
    15. z = np.stack([x, y], axis=2)
    16. print(z.shape) # (1, 3, 2)
    17. print(z)
    18. # [[[1 7]
    19. # [2 8]
    20. # [3 9]]]

    【例】

    1. import numpy as np
    2. x = np.array([[1, 2, 3], [4, 5, 6]])
    3. y = np.array([[7, 8, 9], [10, 11, 12]])
    4. z = np.stack([x, y])
    5. print(z.shape) # (2, 2, 3)
    6. print(z)
    7. # [[[ 1 2 3]
    8. # [ 4 5 6]]
    9. #
    10. # [[ 7 8 9]
    11. # [10 11 12]]]
    12. z = np.stack([x, y], axis=1)
    13. print(z.shape) # (2, 2, 3)
    14. print(z)
    15. # [[[ 1 2 3]
    16. # [ 7 8 9]]
    17. #
    18. # [[ 4 5 6]
    19. # [10 11 12]]]
    20. z = np.stack([x, y], axis=2)
    21. print(z.shape) # (2, 3, 2)
    22. print(z)
    23. # [[[ 1 7]
    24. # [ 2 8]
    25. # [ 3 9]]
    26. #
    27. # [[ 4 10]
    28. # [ 5 11]
    29. # [ 6 12]]]
    • numpy.vstack(tup)Stack arrays in sequence vertically (row wise).
    • numpy.hstack(tup)Stack arrays in sequence horizontally (column wise).

    【例】一维的情况。

    1. import numpy as np
    2. x = np.array([1, 2, 3])
    3. y = np.array([7, 8, 9])
    4. z = np.vstack((x, y))
    5. print(z.shape) # (2, 3)
    6. print(z)
    7. # [[1 2 3]
    8. # [7 8 9]]
    9. z = np.stack([x, y])
    10. print(z.shape) # (2, 3)
    11. print(z)
    12. # [[1 2 3]
    13. # [7 8 9]]
    14. z = np.hstack((x, y))
    15. print(z.shape) # (6,)
    16. print(z)
    17. # [1 2 3 7 8 9]
    18. z = np.concatenate((x, y))
    19. print(z.shape) # (6,)
    20. print(z) # [1 2 3 7 8 9]

    【例】二维的情况。

    1. import numpy as np
    2. x = np.array([1, 2, 3]).reshape(1, 3)
    3. y = np.array([7, 8, 9]).reshape(1, 3)
    4. z = np.vstack((x, y))
    5. print(z.shape) # (2, 3)
    6. print(z)
    7. # [[1 2 3]
    8. # [7 8 9]]
    9. z = np.concatenate((x, y), axis=0)
    10. print(z.shape) # (2, 3)
    11. print(z)
    12. # [[1 2 3]
    13. # [7 8 9]]
    14. z = np.hstack((x, y))
    15. print(z.shape) # (1, 6)
    16. print(z)
    17. # [[ 1 2 3 7 8 9]]
    18. z = np.concatenate((x, y), axis=1)
    19. print(z.shape) # (1, 6)
    20. print(z)
    21. # [[1 2 3 7 8 9]]

    【例】二维的情况。

    1. import numpy as np
    2. x = np.array([[1, 2, 3], [4, 5, 6]])
    3. y = np.array([[7, 8, 9], [10, 11, 12]])
    4. z = np.vstack((x, y))
    5. print(z.shape) # (4, 3)
    6. print(z)
    7. # [[ 1 2 3]
    8. # [ 4 5 6]
    9. # [ 7 8 9]
    10. # [10 11 12]]
    11. z = np.concatenate((x, y), axis=0)
    12. print(z.shape) # (4, 3)
    13. print(z)
    14. # [[ 1 2 3]
    15. # [ 4 5 6]
    16. # [ 7 8 9]
    17. # [10 11 12]]
    18. z = np.hstack((x, y))
    19. print(z.shape) # (2, 6)
    20. print(z)
    21. # [[ 1 2 3 7 8 9]
    22. # [ 4 5 6 10 11 12]]
    23. z = np.concatenate((x, y), axis=1)
    24. print(z.shape) # (2, 6)
    25. print(z)
    26. # [[ 1 2 3 7 8 9]
    27. # [ 4 5 6 10 11 12]]

    hstack(),vstack()分别表示水平和竖直的拼接方式。在数据维度等于1时,比较特殊。而当维度大于或等于2时,它们的作用相当于concatenate,用于在已有轴上进行操作。
    【例】

    1. import numpy as np
    2. a = np.hstack([np.array([1, 2, 3, 4]), 5])
    3. print(a) # [1 2 3 4 5]
    4. a = np.concatenate([np.array([1, 2, 3, 4]), 5])
    5. print(a)
    6. # all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)