Splitting Arrays in NumPy

Learn how to split NumPy arrays using functions like `np.split()`, `np.array_split()`, `np.hsplit()`, `np.vsplit()`, and `np.dsplit()`. A beginner-friendly guide with practical examples.

Getting Started with NumPy Arrays

NumPy is a powerful, open-source library for scientific computing in Python. It is popular for its efficient handling of multi-dimensional arrays, an essential data structure for performing numerical calculations.

In this guide, we’ll discuss in detail how to split NumPy arrays using several functions like np.split(), np.array_split(), np.hsplit(), np.vsplit(), and np.dsplit().

How to Split NumPy Arrays

Array splitting in NumPy refers to breaking a single array into multiple arrays.

The two functions that are mainly used to split a NumPy array include:

  • np.split()
  • np.array_split()

We’ll go through each of them individually.

How to Split NumPy Arrays Using np.split()

Let’s start with the np.split() function, which splits a NumPy array equally along an existing axis (horizontal or vertical). A real-world analogy would be cutting a pizza into equal slices horizontally or vertically.

Before looking at an example, let’s see the syntax for np.split():

np.split(arr, sections, axis=0)

In this syntax:

  • arr: Specifies the array to split
  • sections: Specifies the number of splits
  • axis: Specifies the axis for splitting

When axis is set to 0 (default), the array will be split along the rows (vertical splitting). When we set axis to 1, the array is split along the columns (horizontal splitting). If the value is greater than 1, the split is performed only if the array contains the corresponding dimension. If not, an empty array is returned. Moreover, if we don’t provide a value for the axis, the default value is used.

Now, let’s look at an example:

import numpy as np
# Creating a NumPy array
arr1 = np.array([12, 23, 34, 45, 56, 67])
# Splitting the array
arr2 = np.split(arr1, 3, axis=0)
print(arr2)

Since the input array is 1D, we can only split it vertically along the only dimension (axis=0). If we set axis to 1, we get an error in the output as axis=1 means splitting horizontally along the second dimension, which doesn’t exist.

Here’s the output of the example code:

[array([12, 23]), array([34, 45]), array([56, 67])]

How to Split NumPy Arrays Using np.array_split()

The next function we will discuss is the np.array_split() function. It works just like np.split(), with the added functionality of np.array_split() allowing splits to be of unequal sizes.

Before seeing an example, let’s look at the syntax for np.array_split():

np.array_split(arr, sections, axis=0)

Now, let’s explore how np.array_split() works with the help of an example:

import numpy as np
# Creating a NumPy array
arr1 = np.array([12, 23, 34, 45, 56])
# Splitting the array
arr2 = np.array_split(arr1, 3, axis=0)
print(arr2)

Here is the output:

[array([12, 23]), array([34, 45]), array([56])]

How to Split NumPy Arrays Using np.hsplit()

We can use the np.hsplit() function to split a NumPy array along the columns or the horizontal axis.

First, let’s have a look at the syntax:

np.hsplit(arr, sections)

Let’s walk through an example to understand this function:

import numpy as np
# Creating a NumPy array
arr1 = np.array([[12, 23, 34], [45, 56, 67]])
# Splitting the array
arr2 = np.hsplit(arr1, 3)
print(arr2)

This code produces the following output:

[array([[12],
[45]]), array([[23],
[56]]), array([[34],
[67]])]

How to Split NumPy Arrays Using np.vsplit()

The np.vsplit() function splits a NumPy array along the rows or the vertical axis. This function only works on 2D or higher dimensional arrays.

Here is the syntax:

np.vsplit(arr, sections)

Let’s look at an example:

import numpy as np
# Creating a NumPy array
arr1 = np.array([[12, 23, 34], [45, 56, 67]])
# Splitting the array
arr2 = np.vsplit(arr1, 2)
print(arr2)

Here’s the output for the example code:

[array([[12, 23, 34]]), array([[45, 56, 67]])]

How to Split NumPy Arrays Using np.dsplit()

The np.dsplit() function splits a NumPy array along the depth or height. This function only works on 3D or higher dimensional arrays.

Here is the syntax for np.dsplit():

np.dsplit(arr, sections)

Let’s see an example:

import numpy as np
# Creating a NumPy array
arr1 = np.array([[[12, 23, 34], [45, 56, 67]], [[78, 89, 90], [13, 24, 35]]])
# Splitting the array
arr2 = np.dsplit(arr1, 3)
print(arr2)

Following is the output for the example code:

[array([[[12],
[45]],
[[78],
[13]]]), array([[[23],
[56]],
[[89],
[24]]]), array([[[34],
[67]],
[[90],
[35]]])]

Concept Review and Next Steps

In this article, we discussed NumPy arrays and a range of functions that allow us to perform array splitting in NumPy.

Splitting arrays is an essential operation in data workflows, helping us to solve challenging problems more efficiently. By mastering these operations, we’ll be more capable of handling complex data analysis and scientific computing tasks with confidence.

To learn more about joining arrays in NumPy, check out the Joining Arrays in NumPy for Begineers article on Codecademy. This article teaches you how to join NumPy arrays using functions like np.concatenate(), np.stack(), np.hstack(), np.vstack(), and np.dstack().

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team