• numpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays as views into ary.

    【例】拆分数组。

    1. import numpy as np
    2. x = np.array([[11, 12, 13, 14],
    3. [16, 17, 18, 19],
    4. [21, 22, 23, 24]])
    5. y = np.split(x, [1, 3])
    6. print(y)
    7. # [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
    8. # [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
    9. y = np.split(x, [1, 3], axis=1)
    10. print(y)
    11. # [array([[11],
    12. # [16],
    13. # [21]]), array([[12, 13],
    14. # [17, 18],
    15. # [22, 23]]), array([[14],
    16. # [19],
    17. # [24]])]
    • numpy.vsplit(ary, indices_or_sections) Split an array into multiple sub-arrays vertically (row-wise).

    【例】垂直切分是把数组按照高度切分

    1. import numpy as np
    2. x = np.array([[11, 12, 13, 14],
    3. [16, 17, 18, 19],
    4. [21, 22, 23, 24]])
    5. y = np.vsplit(x, 3)
    6. print(y)
    7. # [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
    8. y = np.split(x, 3)
    9. print(y)
    10. # [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
    11. y = np.vsplit(x, [1])
    12. print(y)
    13. # [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
    14. # [21, 22, 23, 24]])]
    15. y = np.split(x, [1])
    16. print(y)
    17. # [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
    18. # [21, 22, 23, 24]])]
    19. y = np.vsplit(x, [1, 3])
    20. print(y)
    21. # [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
    22. # [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
    23. y = np.split(x, [1, 3], axis=0)
    24. print(y)
    25. # [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
    26. # [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
    • numpy.hsplit(ary, indices_or_sections) Split an array into multiple sub-arrays horizontally (column-wise).

    【例】水平切分是把数组按照宽度切分。

    1. import numpy as np
    2. x = np.array([[11, 12, 13, 14],
    3. [16, 17, 18, 19],
    4. [21, 22, 23, 24]])
    5. y = np.hsplit(x, 2)
    6. print(y)
    7. # [array([[11, 12],
    8. # [16, 17],
    9. # [21, 22]]), array([[13, 14],
    10. # [18, 19],
    11. # [23, 24]])]
    12. y = np.split(x, 2, axis=1)
    13. print(y)
    14. # [array([[11, 12],
    15. # [16, 17],
    16. # [21, 22]]), array([[13, 14],
    17. # [18, 19],
    18. # [23, 24]])]
    19. y = np.hsplit(x, [3])
    20. print(y)
    21. # [array([[11, 12, 13],
    22. # [16, 17, 18],
    23. # [21, 22, 23]]), array([[14],
    24. # [19],
    25. # [24]])]
    26. y = np.split(x, [3], axis=1)
    27. print(y)
    28. # [array([[11, 12, 13],
    29. # [16, 17, 18],
    30. # [21, 22, 23]]), array([[14],
    31. # [19],
    32. # [24]])]
    33. y = np.hsplit(x, [1, 3])
    34. print(y)
    35. # [array([[11],
    36. # [16],
    37. # [21]]), array([[12, 13],
    38. # [17, 18],
    39. # [22, 23]]), array([[14],
    40. # [19],
    41. # [24]])]
    42. y = np.split(x, [1, 3], axis=1)
    43. print(y)
    44. # [array([[11],
    45. # [16],
    46. # [21]]), array([[12, 13],
    47. # [17, 18],
    48. # [22, 23]]), array([[14],
    49. # [19],
    50. # [24]])]