How to Split NumPy Array | Using split()

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 14, 2023
  • Reading time:17 mins read

How to split an array into multiple arrays in Numpy? In NumPy, the numpy.split() function can be used to split an array into more than one (multiple) sub arrays as views. This function divides the array into subarrays along with a specified axis. The function takes three parameters array, indices_or_sections, and axis.

In this article, I will explain with examples how to split Python NumPy array by using numpy.split() function.

1. Quick Examples of NumPy Array Split

If you are in a hurry, below are some quick examples of how to use the NumPy array split() function.


# Quick examples of numpy array split

# Example 1: Use numpy.split() function
arr = np.array([5,7,9,11,13,19,23,27])
subarrays = np.split(arr,4)  

# Example 2: Using numpy.split() function
# To split the array into subarrays
arr = np.array([5,7,9,11,13,19,23,27])
subarrays = np.split(arr, [2, 5, 7])

# Example 3: Use split array along axis = 0
arr = np.array([[15,28,57,65],[25,37,55,88]])
subarrays = np.split(arr, 2, axis=0)

# Example 4: Use split array along axis = 1
arr = np.array([[15,28,57,65],[25,37,55,88]])
subarrays = np.split(arr, 2, axis=1)

# Example 5: Use split array columns via slicing
arr = np.array([[15,28,57,65],[25,37,55,88]])
subarrays = np.split(arr, (2,3), axis=1)

# Example 6: Use numpy.split() function
# Returning value error
arr = np.array([5,7,9,11,13,19,23,27])
subarrays = np.split(arr,5)

2. Syntax of array split()

Following is the syntax of the numpy.split() function.


# Syntax numpy.split() 
numpy.split(array, indices_or_sections, axis=0)

2.1 Parameters of split()

Following are the parameters of split() function.

  • arr – Array to be divided into sub-arrays.
  • indices_or_sections – If it’s an integer, it indicates the number of equal division points. If it’s a 1-D array of sorted integers, the entries indicate the split points.
  • axis – (Optional) The axis along which arr is split. The default value is 0, indicating the first axis. If you want to split along columns, set axis=1

2.2 Return Value of split()

It returns a list of sub-arrays. If indices_or_sections is given as an integer, but it’s unable to split in equal division, it raises a ValueError.

3. Use numpy split()

You can use the numpy.split() function to split an array into multiple subarrays. Below are some points to note while splitting the array.

  • Equal Splitting: When using a scalar integer value for indices_or_sections, the array is divided into equally shaped sub-arrays along the specified axis. If the input array’s shape is not evenly divisible by the number of sections, it may raise an error.
  • Custom Splitting: Providing an array of indices specifies the positions at which the array should be split. For instance, passing [3, 5] would split the array at indices 3 and 5.
  • Splitting Along Different Axes: By adjusting the axis parameter, you can split the array along different axes, such as rows (axis=0) or columns (axis=1).
  • Handling Remainder: If the array cannot be evenly divided, the behavior depends on the axis and the method used for splitting. For example, the numpy.array_split() function allows uneven splits and handles any remaining elements.
  • Efficiency: While numpy.split() divides the array into equal-sized sub-arrays, numpy.array_split() can handle uneven splits.

# Import numpy
import numpy as np
 
# Creating an input array
arr = np.array([5,7,9,11,13,19,23,27])
print ("Creating an input array:\n", arr) 

# Use numpy.split() function
subarrays = np.split(arr,4)  
print ("After splitting array:\n", subarrays) 

Yields below output. Here, the numpy.split() function is used to split the input array arr into 4 equal-sized subarrays. In this case, the input array arr has 8 elements. The np.split() function divides it into 4 equal subarrays.

numpy array split

As you can see, the input array has been split into four subarrays, each containing two elements.

4. Use split() Function to Split 1-D Array

The numpy.split() function can be used to split a 1-D array into multiple subarrays. For instance, the numpy.split() function is used to split the input array arr at the indices [2, 5, 8]. This means the array will be split into four subarrays: Elements from index 0 to 1: [5, 7]. Elements from index 2 to 4: [9, 11, 13]. Elements from index 5 to 6: [19, 23]. Elements from index 7 to the end: [27].


import numpy as np

# Creating a 1-D input array
arr = np.array([5,7,9,11,13,19,23,27])
print("Creating an input array:\n", arr)

# Using numpy.split() function
# To split the array into subarrays
subarrays = np.split(arr, [2, 5, 7])
print("After splitting array:\n", subarrays)

Yields below output.

numpy array split

5. Split 2-D Array Use split() Function

You can use the numpy.split() function to split a 2-D array along a specified axis. Use numpy.split() function to split an array into more than one sub-array vertically (row-wise). There are two ways to split the array one is row-wise and the other is column-wise. By default, the array is split in row-wise (axis=0).


import numpy as np

# Creating an 2D input array
arr = np.array([[15,28,57,65],[25,37,55,88]])
print("Creating an input 2-D array:\n", arr)

# Use split array along axis = 0
subarrays = np.split(arr, 2, axis=0)
print("After splitting 2-D array along axis 0:\n", subarrays)

# Output:
# Creating an input 2-D array:
#  [[15 28 57 65]
#  [25 37 55 88]]
# After splitting 2-D array along axis 0:
#  [array([[15, 28, 57, 65]]), array([[25, 37, 55, 88]])]

You can also use numpy.split() function to split an array into multiple sub-arrays horizontally (column-wise). You can perform a horizontal split with the numpy.split() function. By using axis=1 along with the input array and the number of sections to split.


import numpy as np

# Creating an 2D input array
arr = np.array([[15,28,57,65],[25,37,55,88]])
print("Creating an input 2-D array:\n", arr)

# Use split array along axis = 1
subarrays = np.split(arr, 2, axis=1)
print("After splitting 2-D array along axis 1:\n", subarrays)

# Output:
# Creating an input 2-D array:
#  [[15 28 57 65]
#  [25 37 55 88]]
# After splitting 2-D array along axis 1:
#  [array([[15, 28],
#        [25, 37]]), array([[57, 65],
#        [55, 88]])]

To create a 2-D NumPy array and then use slicing along the columns to split the array into subarrays. In this code, the np.split() function is not directly used. Instead, the splitting is achieved by slicing the array along axis 1 at the indices (2, 3).


import numpy as np

# Creating an 2D input array
arr = np.array([[15,28,57,65],[25,37,55,88]])

# Use split array columns via slicing
subarrays = np.split(arr, (2,3), axis=1)
print("After splitting array columns via slicing:\n", subarrays)

# Output:
# After splitting array columns via slicing:
#  [array([[15, 28],
#        [25, 37]]), array([[57],
#        [55]]), array([[65],
#        [88]])]

6. Split() Returning ValueError

If split() function is unable to split in an equal division it returns a ValueError: array split does not result in an equal division. In our example below, I am trying to split 8 elements into 5 slices which is not possible hence it returns an error.


import numpy as np

# Creating an input array
arr = np.array([5,7,9,11,13,19,23,27])

# Use numpy.split() function
# Returning valueError
subarrays = np.split(arr,5)  
print("After splitting array:\n", subarrays)

# Output:
# ValueError: array split does not result in an equal division

Frequently Asked Questions

What does the numpy.split() function do?

The numpy.split() function in the NumPy library is used to split an array into multiple subarrays along a specified axis. The function takes three parameters array, indices_or_sections, and axis.

How can I split a 1-D array into equal parts using numpy.split()?

You can split a 1-D array into equal parts using numpy.split(), you need to specify the number of parts you want the array to be divided into.

Split a 2-D array into rows and columns using numpy.split()

You can split a 2-D array into rows and columns using numpy.split(). You can specify the axis along which you want to split the array. If you want to split along rows, you can set axis=0. If you want to split along columns, you can set axis=1.

Can I split an array at specific indices using numpy.split()?

You can split an array at specific indices using numpy.split(). When you use numpy.split(), you provide a list of indices where you want to split the array.

What happens if the array size is not divisible by the number of parts in numpy.split()?

If the array size is not divisible by the number of parts, numpy.split() will raise a ValueError because it cannot create equal-sized subarrays. Make sure the array size is compatible with the number of parts.

Conclusion

In NumPy, the split() function is used to split an array into multiple sub-arrays along a specified axis. When using a scalar integer value for indices_or_sections, the array is divided into equally shaped sub-arrays along the specified axis. If the input array’s shape is not evenly divisible by the number of sections, it may raise an error. Providing an array of indices specifies the positions at which the array should be split. For instance, passing [3, 5] would split the array at indices 3 and 5.

Happy Learning!!

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply