Skip to main content

split

Split an array into multiple sub-arrays as views into the original array.
numpy.split(ary, indices_or_sections, axis=0)
ary
ndarray
Array to be divided into sub-arrays.
indices_or_sections
int or 1-D array
If an integer, N, the array will be divided into N equal arrays along axis. If such a split is not possible, an error is raised.If a 1-D array of sorted integers, the entries indicate where along axis the array is split. For example, [2, 3] would result in:
  • ary[:2]
  • ary[2:3]
  • ary[3:]
axis
int
default:"0"
The axis along which to split, default is 0.
Returns: list of ndarrays - A list of sub-arrays as views into ary.

Examples

import numpy as np

# Split into 3 equal parts
x = np.arange(9.0)
print(np.split(x, 3))
# [array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]

# Split at specific indices
x = np.arange(8.0)
print(np.split(x, [3, 5, 6, 10]))
# [array([0., 1., 2.]),
#  array([3., 4.]),
#  array([5.]),
#  array([6., 7.]),
#  array([], dtype=float64)]

# Split 2-D array along axis 1
x = np.arange(12).reshape(3, 4)
print(np.split(x, 2, axis=1))
# [array([[0, 1],
#         [4, 5],
#         [8, 9]]),
#  array([[ 2,  3],
#         [ 6,  7],
#         [10, 11]])]

See Also

  • array_split - Split array into multiple sub-arrays of equal or near-equal size
  • hsplit - Split array horizontally
  • vsplit - Split array vertically
  • dsplit - Split array along the 3rd axis

array_split

Split an array into multiple sub-arrays.
numpy.array_split(ary, indices_or_sections, axis=0)
ary
ndarray
Array to be divided into sub-arrays.
indices_or_sections
int or 1-D array
If an integer that does not equally divide the axis, the first l % n sub-arrays will have size l//n + 1, and the rest will have size l//n.
axis
int
default:"0"
The axis along which to split, default is 0.
Returns: list of ndarrays - A list of sub-arrays as views into ary.

Examples

import numpy as np

# Split into 3 parts (not evenly divisible)
x = np.arange(8.0)
print(np.array_split(x, 3))
# [array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7.])]

# Split into 4 parts
x = np.arange(9)
print(np.array_split(x, 4))
# [array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]

See Also

  • split - Split array into multiple sub-arrays of equal size

hsplit

Split an array into multiple sub-arrays horizontally (column-wise).
numpy.hsplit(ary, indices_or_sections)
ary
ndarray
Array to be divided into sub-arrays.
indices_or_sections
int or 1-D array
If an integer, N, the array will be divided into N equal arrays along axis 1 (or axis 0 for 1-D arrays). If a 1-D array of sorted integers, the entries indicate where to split the array.
Returns: list of ndarrays - A list of sub-arrays as views into ary.

Examples

import numpy as np

# Split 2-D array horizontally
x = np.arange(16.0).reshape(4, 4)
print(x)
# array([[ 0.,  1.,  2.,  3.],
#        [ 4.,  5.,  6.,  7.],
#        [ 8.,  9., 10., 11.],
#        [12., 13., 14., 15.]])

print(np.hsplit(x, 2))
# [array([[ 0.,  1.],
#         [ 4.,  5.],
#         [ 8.,  9.],
#         [12., 13.]]),
#  array([[ 2.,  3.],
#         [ 6.,  7.],
#         [10., 11.],
#         [14., 15.]])]

# Split at specific columns
print(np.hsplit(x, np.array([3, 6])))
# [array([[ 0.,  1.,  2.],
#         [ 4.,  5.,  6.],
#         [ 8.,  9., 10.],
#         [12., 13., 14.]]),
#  array([[ 3.],
#         [ 7.],
#         [11.],
#         [15.]]),
#  array([], shape=(4, 0), dtype=float64)]

# 1-D arrays split along axis 0
x = np.array([0, 1, 2, 3, 4, 5])
print(np.hsplit(x, 2))
# [array([0, 1, 2]), array([3, 4, 5])]

See Also

  • split - Split array into multiple sub-arrays of equal size

vsplit

Split an array into multiple sub-arrays vertically (row-wise).
numpy.vsplit(ary, indices_or_sections)
ary
ndarray
Array to be divided into sub-arrays. Must be at least 2-D.
indices_or_sections
int or 1-D array
If an integer, N, the array will be divided into N equal arrays along axis 0. If a 1-D array of sorted integers, the entries indicate where to split the array.
Returns: list of ndarrays - A list of sub-arrays as views into ary.

Examples

import numpy as np

# Split 2-D array vertically
x = np.arange(16.0).reshape(4, 4)
print(x)
# array([[ 0.,  1.,  2.,  3.],
#        [ 4.,  5.,  6.,  7.],
#        [ 8.,  9., 10., 11.],
#        [12., 13., 14., 15.]])

print(np.vsplit(x, 2))
# [array([[0., 1., 2., 3.],
#         [4., 5., 6., 7.]]),
#  array([[ 8.,  9., 10., 11.],
#         [12., 13., 14., 15.]])]

# Split at specific rows
print(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([], shape=(0, 4), dtype=float64)]

# 3-D arrays split along first axis
x = np.arange(8.0).reshape(2, 2, 2)
print(np.vsplit(x, 2))
# [array([[[0., 1.],
#          [2., 3.]]]),
#  array([[[4., 5.],
#          [6., 7.]]])]

See Also

  • split - Split array into multiple sub-arrays of equal size

dsplit

Split array into multiple sub-arrays along the 3rd axis (depth).
numpy.dsplit(ary, indices_or_sections)
ary
ndarray
Array to be divided into sub-arrays. Must be at least 3-D.
indices_or_sections
int or 1-D array
If an integer, N, the array will be divided into N equal arrays along axis 2. If a 1-D array of sorted integers, the entries indicate where to split the array.
Returns: list of ndarrays - A list of sub-arrays as views into ary.

Examples

import numpy as np

# Split 3-D array along depth
x = np.arange(16.0).reshape(2, 2, 4)
print(x)
# array([[[ 0.,  1.,  2.,  3.],
#         [ 4.,  5.,  6.,  7.]],
#        [[ 8.,  9., 10., 11.],
#         [12., 13., 14., 15.]]])

print(np.dsplit(x, 2))
# [array([[[ 0.,  1.],
#          [ 4.,  5.]],
#         [[ 8.,  9.],
#          [12., 13.]]]),
#  array([[[ 2.,  3.],
#          [ 6.,  7.]],
#         [[10., 11.],
#          [14., 15.]]])]

# Split at specific indices
print(np.dsplit(x, np.array([1, 3])))
# [array([[[ 0.],
#          [ 4.]],
#         [[ 8.],
#          [12.]]]),
#  array([[[ 1.,  2.],
#          [ 5.,  6.]],
#         [[ 9., 10.],
#          [13., 14.]]]),
#  array([[[ 3.],
#          [ 7.]],
#         [[11.],
#          [15.]]])]

See Also

  • split - Split array into multiple sub-arrays of equal size
  • hsplit - Split array horizontally
  • vsplit - Split array vertically

Build docs developers (and LLMs) love