Python NumPy vsplit() Function

NumPy vsplit() Function:

The vsplit() function of the Numpy module is used to split an array into multiple sub-arrays vertically (row-wise).

Note that vsplit is similar to split with axis=0 (the default); regardless of the array dimension, the array is always split along the first axis.

Syntax:

numpy.vsplit(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 vertically 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 vertically.
  • If an index exceeds the vertical dimension of the array, an empty sub-array is returned correspondingly.

Return Value: 

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

NumPy vsplit() 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 vsplit() function to vertically split the array.
  • Store it in another variable.
  • Print the above vertically 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 vsplit()function to 
# vertically split the array
# Store it in another variable.
splt_arry = np.vsplit(gvn_arry, 3)
# Print the above vertically split Array
print("The above vertically split Array is:")
print(splt_arry)

Output:

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

Example2

When indices_or_sections is given as a 1-dimensional(1D)array of sorted integers, the elements indicate where the array is split vertically.

# 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 vsplit() to 
# vertically  split the array
# Store it in another variable.
splt_arry = np.vsplit(gvn_arry, [1,2])
# Print the above vertically split Array
print("The above vertically 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 vertically split Array is:
[array([[11, 12, 13, 14]]), array([[15, 16, 17, 18]]), array([[19, 20, 21, 22]])]