In this tutorial, we will look at the numpy vsplit() function and its usage with the help of some examples.

## What is does numpy vsplit() do?

The numpy vsplit() function is used to split a numpy array into multiple sub-arrays vertically (row-wise). Pass the input array and the number of sub-arrays as arguments. The following is the syntax:

import numpy as np # split array row-wise (vertically) sub_arrays = np.vsplit(arr, indices_or_sections)

It returns a list of numpy arrays created from the split (the sub-arrays).

Let’s look at some examples of using the numpy vsplit() function.

## Vertically split a 2d numpy array

Let’s split a 2d array of shape (4, 3) at the middle into two sub-arrays of shape (2, 3) each.

import numpy as np # create a 2d numpy array arr = np.array([[1, 2, 2], [2, 0, 0], [3, 1, 1], [4, 0, 4]]) # split the array into 2 subarrays vertically sub_arrays = np.vsplit(arr, 2) # display the sub_arrays sub_arrays

Output:

[array([[1, 2, 2], [2, 0, 0]]), array([[3, 1, 1], [4, 0, 4]])]

You can see that the input array of shape (4, 3) has been split into two halves row-wise (vertically) with subarrays of shape (2, 3).

Let’s now split the same input array into 4 sub-arrays of (1, 3) by splitting all the rows.

# create a 2d numpy array arr = np.array([[1, 2, 2], [2, 0, 0], [3, 1, 1], [4, 0, 4]]) # split the array into 4 subarrays vertically sub_arrays = np.vsplit(arr, 4) # display the sub_arrays sub_arrays

Output:

[array([[1, 2, 2]]), array([[2, 0, 0]]), array([[3, 1, 1]]), array([[4, 0, 4]])]

We get four (1, 3) sub-arrays.

## Using numpy split() with axis=0

Alternatively, you can perform a vertical split with the numpy `split()`

function. Just pass `axis=0`

along with the input array and the number of sections to split it into.

Let’s split the above 2d array into two sub-arrays vertically but using the numpy `split()`

function this time.

# create a 2d numpy array arr = np.array([[1, 2, 2], [2, 0, 0], [3, 1, 1], [4, 0, 4]]) # split the array into 2 subarrays vertically sub_arrays = np.split(arr, 2, axis=0) # display the sub_arrays sub_arrays

Output:

[array([[1, 2, 2], [2, 0, 0]]), array([[3, 1, 1], [4, 0, 4]])]

We get the same result as we did with the vsplit() function.

For more on the numpy vsplit() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**

Tutorials on numpy arrays –

- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()