Mastering Array Division: A Guide to NumPy’s Split Functions


link to this section

NumPy, a cornerstone library in Python for numerical computing, provides an extensive range of functionalities for array manipulations. Among these, the split functions are crucial when you need to divide an array into multiple sub-arrays. This blog post offers a detailed exploration of the various split functions in NumPy: split , array_split , hsplit , vsplit , and dsplit , providing clear examples, applications, and best practices.

NumPy’s Split Functions Overview

link to this section
  1. split : Splits an array into multiple sub-arrays of equal size (if possible).
  2. array_split : Similar to split , but allows for indices that do not divide the array equally.
  3. hsplit : Splits an array horizontally (column-wise).
  4. vsplit : Splits an array vertically (row-wise).
  5. dsplit : Splits an array across the third axis (depth-wise).

Basic Usage of split

link to this section

The split function divides an array into multiple sub-arrays:

numpy.split(ary, indices_or_sections, axis=0) 
  • ary : The array to be divided.
  • indices_or_sections : If an integer, the array will be divided into that many equally sized arrays. If an array, the integers in the array represent the positions at which to split.
  • axis : The axis along which to split.

Example: Splitting a 1-D Array

import numpy as np 
array = np.array([1, 2, 3, 4, 5, 6]) 
sub_arrays = np.split(array, 3) 


[array([1, 2]), 
array([3, 4]), 
array([5, 6])] 

Here, the 1-D array is split into 3 equal parts.

Using array_split

link to this section

The array_split function is similar to split , but it allows for indices that do not evenly divide the array:

sub_arrays = np.array_split(array, 4) 


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

Horizontal and Vertical Splits: hsplit and vsplit

link to this section

Example of hsplit :

array_2d = np.array([[1, 2, 3], [4, 5, 6]]) 
sub_arrays = np.hsplit(array_2d, 3) 


[array([[1], [4]]), 
array([[2], [5]]), 
array([[3], [6]])] 

Example of vsplit :

sub_arrays = np.vsplit(array_2d, 2) 


[array([[1, 2, 3]]), 
array([[4, 5, 6]])] 

Splitting Along the Third Axis: dsplit

link to this section

The dsplit function is useful for 3-dimensional arrays, where you wish to split along the depth:

array_3d = np.arange(27).reshape((3, 3, 3)) 
sub_arrays = np.dsplit(array_3d, 3) 


[[[ 0 1 2]] 
[[ 9 10 11]] 
[[18 19 20]]] 

Applications in Data Science

link to this section
  • Batch Processing : Dividing data into batches for training machine learning models.
  • Cross-Validation : Splitting data for cross-validation in model evaluation.
  • Image Processing : Dividing images into patches for detailed analysis.


link to this section

NumPy’s split functions offer a versatile set of options for dividing arrays, catering to various requirements and use cases in data manipulation and analysis. Through this comprehensive guide, you have learned how to effectively utilize these functions, grasping their syntax, behavior, and practical applications. Whether you are working with 1-D arrays, multi-dimensional data, or dealing with specific applications like image processing, you are now well-equipped to handle array division tasks in Python with confidence and precision. Happy splitting!