NumPy - vsplit() function
The NumPy vsplit() function splits an array into multiple sub-arrays vertically (row-wise).
Syntax
numpy.vsplit(ary, indices_or_sections)
Parameters
ary |
Required. Specify the array (ndarray) to be divided into sub-arrays. |
indices_or_sections |
Required. Specify indices_or_sections as int or 1-D array.
|
Return Value
Returns a list of sub-arrays as views into ary.
Example:
In the example below, vsplit() function is used to split a given array.
import numpy as np Arr = np.array([[10, 20, 30], [30, 40, 60], [70, 80, 90]]) #splitting the array Arr1 = np.vsplit(Arr, 3) #displaying results print("Arr is:") print(Arr) print("\nArr1 is:") print(Arr1)
The output of the above code will be:
Arr is: [[10 20 30] [30 40 60] [70 80 90]] Arr1 is: [array([[10, 20, 30]]), array([[30, 40, 60]]), array([[70, 80, 90]])]
Example: indices_or_sections as 1-D array
When indices_or_sections is passed as 1-D array of sorted integers, the entries indicate where the array is split vertically. Consider the following example.
import numpy as np Arr = np.array([[10, 20, 30], [30, 40, 60], [70, 80, 90], [100, 200, 300]]) #splitting the array Arr1 = np.vsplit(Arr, [2,3]) #displaying results print("Arr is:") print(Arr) print("\nArr1 is:") print(Arr1)
The output of the above code will be:
Arr is: [[ 10 20 30] [ 30 40 60] [ 70 80 90] [100 200 300]] Arr1 is: [array([[10, 20, 30], [30, 40, 60]]), array([[70, 80, 90]]), array([[100, 200, 300]])]
❮ NumPy - Functions