Splitting Arrays in NumPy

• Last Updated : 10 May, 2020

Array splitting can be vertical, horizontal, or depth-wise. We can use functions hsplit(), vsplit() and dsplit() respectively for the same . We can split arrays into arrays of the same shape by indicating the position after which the split should occur.

• Horizontal splitting: The ‘hsplit()’ function splits an array along axis parameter = 1. ‘numpy.hsplit’ is equivalent to ‘split’ with axis parameter = 1, the array is always splitted along the second axis regardless of the array dimension. This function split an array into multiple sub-arrays horizontally (column-wise).

Syntax:

numpy.hsplit(ary, indices_or_sections)

Example:

 # Horizontal array splitting using np.hsplit()import numpy as np    # Making of  a 3x3 arraya = np.arange(9).reshape(3, 3)  # Horizontal splitting of array # 'a' using np.hsplit().print("The array {} gets splitted \horizontally to form {} ".format(a, np.hsplit(a, 3)))  # Horizontal splitting of array 'a' # using 'split' with axis parameter = 1.print("The array {} gets splitted \horizontally to form {} ".format(a, np.split(a, 3, 1)))

Output:

The array [[0 1 2]
[3 4 5]
[6 7 8]] gets splitted horizontally to form [array([,
,
]), array([,
,
]), array([,
,
])]
The array [[0 1 2]
[3 4 5]
[6 7 8]] gets splitted horizontally to form [array([,
,
]), array([,
,
]), array([,
,
])]
• Vertical splitting: The ‘vsplit()’ function splits an array along axis parameter = 0.‘numpy.vsplit’ is equivalent to ‘split’ with axis parameter = 0. This function split an array into multiple sub-arrays vertically (row-wise).
numpy.vsplit(ary, indices_or_sections)

Example:

 import numpy as np    # Making of  a 3x3 arraya = np.arange(9).reshape(3, 3)  # Vertical splitting of array 'a'# using np.vsplit().print("The array {} gets splitted \vertically to form {} ".format(a, np.vsplit(a, 3)))  # Vertical splitting of array 'a' # using 'split' with axis parameter = 0.print("The array {} gets splitted \vertically to form {} ".format(a, np.split(a, 3, 0)))

Output:

The array [[0 1 2]
[3 4 5]
[6 7 8]] gets splitted vertically to form [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
The array [[0 1 2]
[3 4 5]
[6 7 8]] gets splitted vertically to form [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]

• Depth-wise splitting: It Split the array into multiple sub-arrays along the 3rd axis (depth).
numpy.dsplit(ary, indices_or_sections)

Example:

 import numpy as np    # Making of  a 3x3x3 array.b = np.arange(27).reshape(3, 3, 3)  # Depth-wise splitting of array# 'b' using np.dsplit().print("The array {} gets splitted \depth-wise to form {}".format(b, np.dsplit(b, 3)))

Output:

The array [[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]

[[ 9 10 11]
[12 13 14]
[15 16 17]]

[[18 19 20]
[21 22 23]
[24 25 26]]] gets splitted depth-wise to form [array([[[ 0],
[ 3],
[ 6]],

[[ 9],
,
],

[,
,
]]), array([[[ 1],
[ 4],
[ 7]],

[,
,
],

[,
,
]]), array([[[ 2],
[ 5],
[ 8]],

[,
,
],

[,
,
]])]

My Personal Notes arrow_drop_up