Numpy hsplit – Python NumPy hsplit() Function

NumPy hsplit() Function:

Numpy hsplit: The hsplit() function of the Numpy module is used to split an array into multiple sub-arrays horizontally (column-wise).

hsplit is equivalent to split with axis=1, and regardless of the array dimension, the array is always split along the second axis.

Syntax:

numpy.hsplit(array, indices_or_sections)

Parameters

array: This is required. It is the input array to be split into multiple sub-arrays.

indices_or_sections: This is required. It specifies the indices or sections as an int or a 1-D array.

  • If indices_or_sections is an integer, N, the array will be divided horizontally into N equal arrays. An error is raised if such a split is not possible.
  • If indices_or_sections is a one-dimensional(1D) array of sorted integers, the entries indicate where the array is split horizontally.
  • If an index exceeds the horizontal dimension of the array, an empty sub-array is returned correspondingly.

Return Value: 

Hssplit: A list of sub-arrays as views into the given array is returned.

NumPy hsplit() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list(multi-dimensional) as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array, indices value(here it is 3) as an argument to the hsplit() function to horizontally split the array.
  • Store it in another variable.
  • Print the above horizontally split Array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list(multi-dimensional) as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, indices value(here it is 3) as an argument to the hsplit()function to 
# horizontally split the array
# Store it in another variable.
splt_arry = np.hsplit(gvn_arry, 3)
# Print the above horizontally split Array
print("The above horizontally split Array is:")
print(splt_arry)

Output:

The above given array is:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
The above horizontally split Array is:
[array([[1],
            [4],
            [7]]), array([[2],
            [5],
            [8]]), array([[3],
            [6],
            [9]])]

Example2

Hsplit: When indices_or_sections is given as a 1-dimensional array of sorted integers, the elements indicate where the array is split horizontally.

# Import numpy module using the import keyword
import numpy as np
# Pass some random list(multi-dimensional) as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[11, 12, 13, 14],
                   [15, 16, 17, 18],
                   [19, 20, 21, 22]])
                  
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, indices value as 1D array as an argument to the hsplit() to 
# horizontally split the array
# Store it in another variable.
splt_arry = np.hsplit(gvn_arry, [1,2])
# Print the above horizontally split Array
print("The above horizontally split Array is:")
print(splt_arry)

Output:

The above given array is:
[[11 12 13 14]
 [15 16 17 18]
 [19 20 21 22]]
The above horizontally split Array is:
[array([[11],
           [15],
           [19]]), array([[12],
           [16],
           [20]]), array([[13, 14],
           [17, 18],
           [21, 22]])]