NumPy Array Split

In this tutorial you will learn:

  • What is Array split?
  • Array Spliting using array_split() function
  • Array splitting using split() function
  • Array split using hsplit(), vsplit() and dsplit() functions

Array Split

Array splitting is the process opposite to the array joining. In array splitting the given array is distributed into number of predetermined sub arrays, these sub-arrays can be further used by programmers to perform various different tasks. The array splitting is carried out to convert the array as desired by certain function or algorithm. In NumPy the array splitting can be carried out using following five functions

  • Function array_split()
  • Function split()
  • Function hsplit()
  • Function vsplit()
  • Function dsplit()

Array Split

The array_split() function is a built in method of NumPy array and it is used to split multi dimensional array. Function array_split() takes 3 input parameters, first parameter is the given array which is to be split, second paramter is the number of arrays into which we want the given array to be split into while the third parameter is the axis, which if not explicitly declared is equal to 0.

In this example we will split a 1D array into three 1D arrays using array_split()

  1. import numpy as np
  2. my_arr = np.array([1,2,3,4,5,6])
  3. print('1D array being split using array_split():\n', my_arr)
  4. #splitting array
  5. array1 = np.array_split(my_arr,3)
  6. print('Result after applying array_split():\n', array1)

In this example we will be splitting 1D array (1,6) into 4 sub arrays. In results you can clearly observe that the sub arrays formed after splitting are having different size because 6 elements were to be divided into 4 sub arrays and here the equal distribution was not possible. Function array_split() provides facility to programmer to split the array without worrying for the dimensions.

  1. import numpy as np
  2. my_arr = np.array([1,2,3,4,5,6])
  3. print('1D array being split using array_split():\n', my_arr)
  4. #splitting array
  5. array1 = np.array_split(my_arr,4)
  6. print('Result after applying array_split():\n', array1)

Lets look at another example, in this example we will split a 2D array (2,6) into 2 arrays along the same axis.

  1. import numpy as np
  2. my_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
  3. print('2D array
  4. being split using array_split():\n', my_arr)
  5. #splitting arrayarray1 = np.array_split(my_arr,2)
  6. print('Result after applying array_split():\n', array1)

Split Function

Split() performs the same function as that of the array_split() except it raises an error if it finds that the number of sub arrays requested by programmer will result in an unequal distribution of the elements of given array. Function split() takes in 3 parameters, first parameter is the array which is to be split, second parameter is the no of arrays into which we want the array to be split into. Third parameter is the axis along which we want to split our array.

Lets looks at an example, in this example we will split a 1D array(1,6) into 2 1D arrays(1,3). Here you can see that the array(1,6) will be split into two equal sized arrays.

  1. import numpy as np
  2. my_arr = np.array([1,2,3,4,5,6])
  3. print('1D array being split using split():\n', my_arr)
  4. #splitting array
  5. array1 = np.split(my_arr,2)
  6. print('Result after applying split():\n', array1)

In this example we will try to split 1D array (1,6) into 4 1D arrays, but unlike function array_split(), the split() function will generate an error as it does not cater for the uneven distribution of elements in the sub arrays and gives an error “array split does not result in an equal division”

  1. import numpy as np
  2. my_arr = np.array([1,2,3,4,5,6])
  3. print('1D array being split using split():\n', my_arr)
  4. #splitting array
  5. array1 = np.split(my_arr,4)
  6. print('Result after applying split():\n', array1)

In this example I will split a 2D array (2,6) into three 2D arrays (2,2) along an axis by explicitly declaring third parameter (axis=1).

  1. import numpy as np
  2. my_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
  3. print('2D array being split using split():\n', my_arr)
  4. #splitting array
  5. array1 = np.split(my_arr,3,axis=1)
  6. print('Result after applying split():\n', array1)

Functions hsplit, vsplit and dsplit

hsplit,vsplit and dsplit are opposite of hstack,vstack and stack. They are responsible for array splitting into rows, columns and height respectively.

As vsplit() can be performed on a 2D array only and dsplit() can be performed on 3D array only, so in this example I will perform hsplit on 1D Array(1,6)

  1. import numpy as np
  2. my_arr = np.array([1,2,3,4,5,6])
  3. print('1D array being split using hsplit():\n', my_arr)
  4. #splitting array using hsplit()
  5. array1 = np.hsplit(my_arr,2)
  6. print('Result after applying hsplit():\n', array1)

In this example we will split a 2D array (2,6) using hsplit() first and then vsplit().

  1. import numpy as np
  2. my_arr = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
  3. print('2D array being split using hsplit() and vsplit():\n', my_arr)
  4. #splitting array using hsplit()
  5. array1 = np.hsplit(my_arr,2)
  6. print('Result after applying hsplit():\n', array1)
  7. #splitting array using vsplit()
  8. array2 = np.vsplit(my_arr,2)
  9. print('\n Result after applying vsplit():\n', array2)

Lets take a look at another example, in this example we will split a 3D array using hsplit(), vsplit() and dsplit (). Here you can closely observe the changes in dimensions of sub array due to each split

  1. import numpy as np
  2. my_arr = np.array([[[1,2,3,4,5,6],[7,8,9,10,11,12],[24,25,26,27,28,29]]])
  3. print('3D array being split using hsplit(), vsplit() and dsplit ():\n', my_arr)
  4. #splitting array using hsplit()
  5. array1 = np.hsplit(my_arr,3)
  6. print('Result after applying hsplit():\n', array1)
  7. #splitting array using vsplit()
  8. array2 = np.vsplit(my_arr,1)
  9. print('\n Result after applying vsplit():\n', array2)
  10. #splitting array using dsplit()
  11. array3 = np.dsplit(my_arr,3)
  12. print('\n Result after applying dsplit():\n', array3)

Add new comment