numpy.split(ary, indices_or_sections, axis=0)
Split an array into multiple sub-arrays as views into ary.
【例】拆分数组。
import numpy as np
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.split(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]
numpy.vsplit(ary, indices_or_sections)
Split an array into multiple sub-arrays vertically (row-wise).
【例】垂直切分是把数组按照高度切分
import numpy as np
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.vsplit(x, 3)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
y = np.split(x, 3)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]
y = np.vsplit(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]])]
y = np.split(x, [1])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]])]
y = np.vsplit(x, [1, 3])
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]
y = np.split(x, [1, 3], axis=0)
print(y)
# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],
# [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).
【例】水平切分是把数组按照宽度切分。
import numpy as np
x = np.array([[11, 12, 13, 14],
[16, 17, 18, 19],
[21, 22, 23, 24]])
y = np.hsplit(x, 2)
print(y)
# [array([[11, 12],
# [16, 17],
# [21, 22]]), array([[13, 14],
# [18, 19],
# [23, 24]])]
y = np.split(x, 2, axis=1)
print(y)
# [array([[11, 12],
# [16, 17],
# [21, 22]]), array([[13, 14],
# [18, 19],
# [23, 24]])]
y = np.hsplit(x, [3])
print(y)
# [array([[11, 12, 13],
# [16, 17, 18],
# [21, 22, 23]]), array([[14],
# [19],
# [24]])]
y = np.split(x, [3], axis=1)
print(y)
# [array([[11, 12, 13],
# [16, 17, 18],
# [21, 22, 23]]), array([[14],
# [19],
# [24]])]
y = np.hsplit(x, [1, 3])
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]
y = np.split(x, [1, 3], axis=1)
print(y)
# [array([[11],
# [16],
# [21]]), array([[12, 13],
# [17, 18],
# [22, 23]]), array([[14],
# [19],
# [24]])]