IKH

Chepter-11

Splitting of arrays

We can perform split operation on ndarrays using the following functions

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

We will get only views, but not copies because the data is not going to be changed

Split()

Example

Python
In [297]: 
import numpy as np 
help(np.split) 

Output

PowerShell
Help on function split in module numpy: 
 
split(ary, indices_or_sections, axis=0) 
    Split an array into multiple sub-arrays as views into `ary`. 

split(array, indices_or_sections, axis=0)

  • Split an array into multiple sub-arrays of equal size.
  • sections means the number of sub-arrays
  • it returns list of ndarray objects.
  • all sections must be of equal size, otherwise error.

split() based on sections

  • We can split arrays based on sections or indices.
  • If we split based on sections, the sizes of sub-arrays should be equal.
  • If we split based on indices, then the sizes of sub-arrays need not be the same

1-D arrays (axis=0)

Example

Python
In [298]: 
a = np.arange(1,10) 
sub_arrays = np.split(a,3) 
print(f"array a : {a}") 
print(f"Type of sub_arrays :{type(sub_arrays)}") 
print(f"sub_arrays : {sub_arrays}") 

Output

PowerShell
array a : [1 2 3 4 5 6 7 8 9] 
Type of sub_arrays :<class 'list'> 
sub_arrays : [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]

Example

Python
In [299]: 
# If dividing array into equal number of specified sections is not possible, then we 
will get error. 
np.split(a,4) 

Output

Python
--------------------------------------------------------------------------- 
ValueError                                Traceback (most recent call last) 
<ipython-input-299-821b8595e272> in <module> 
      1 # If dividing array into equal number of specified sections is not po
 ssible, then we will get error. ----> 2 np.split(a,4) 
 
<__array_function__ internals> in split(*args, **kwargs) 
 
F:\Users\Gopi\anaconda3\lib\site-packages\numpy\lib\shape_base.py in split(ar
 y, indices_or_sections, axis) 
    870         N = ary.shape[axis] 
    871         if N % sections: --> 872             raise ValueError( 
    873                 'array split does not result in an equal division') f
 rom None 
    874     return array_split(ary, indices_or_sections, axis) 
 
ValueError: array split does not result in an equal division

2-D arrays (axis=0 ==> Vertical split)

  • splitting is based on axis-0 bydefault. ie row wise split(vertical split)
  • We can also split based on axis-1. column wise split (horizontal split)

Example

Python
In [300]: 
# split based on default axis i.e., axis-0 (Vertical Split) 
a = np.arange(1,25).reshape(6,4) 
result_3sections = np.split(a,3) # dividing 3 sections vertically 
print(f"array a : \n {a}") 
print(f"splitting the array into 3 sections along axis-0 : \n {result_3sections}") 
# Note: Here we can use various possible sections: 2,3,6 

Output

PowerShell
array a :  
 [[ 1  2  3  4] 
 [ 5  6  7  8] 
 [ 9 10 11 12] 
 [13 14 15 16] 
 [17 18 19 20] 
 [21 22 23 24]] 
splitting the array into 3 sections along axis-0 :  
 [array([[1, 2, 3, 4], 
       [5, 6, 7, 8]]), array([[ 9, 10, 11, 12], 
       [13, 14, 15, 16]]), array([[17, 18, 19, 20], 
       [21, 22, 23, 24]])]

Example

Python
In [301]: 
# Note: Here we can use various possible sections: 2,3,6 
result_2sections = np.split(a,2,axis=0) 
result_6sections = np.split(a,6,axis=0) 
print(f"splitting the array into 2 sections along axis-0 : \n {result_2sections}") 
print(f"splitting the array into 6 sections along axis-0 : \n {result_6sections}")

Output

PowerShell
splitting the array into 2 sections along axis-0 :  
 [array([[ 1,  2,  3,  4], 
       [ 5,  6,  7,  8], 
       [ 9, 10, 11, 12]]), array([[13, 14, 15, 16], 
       [17, 18, 19, 20], 
       [21, 22, 23, 24]])] 
splitting the array into 6 sections along axis-0 :  
 [array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]]), array([[ 9, 10, 11, 12]]), array([[13
 , 14, 15, 16]]), array([[17, 18, 19, 20]]), array([[21, 22, 23, 24]])] 

2-D arrays (axis=1 ==> Horizontal split)

Example

Python
In [302]: 
# split based on axis-1  (horizontal split) 
# for the shape(6,4) we can perform 4 or 2 sections 
a = np.arange(1,25).reshape(6,4) 
result_2sections = np.split(a,2,axis=1) # dividing 2 sections horizontally 
result_4sections = np.split(a,4,axis=1) # dividing 4 sections horizontally 
 
print(f"splitting the array into 2 sections along axis-0 : \n {result_2sections}") 
print(f"splitting the array into 4 sections along axis-0 : \n {result_4sections}")

Output

PowerShell
splitting the array into 2 sections along axis-0 :  
 [array([[ 1,  2], 
       [ 5,  6], 
       [ 9, 10], 
       [13, 14], 
       [17, 18], 
       [21, 22]]), array([[ 3,  4], 
       [ 7,  8], 
       [11, 12], 
       [15, 16], 
       [19, 20], 
       [23, 24]])] 
splitting the array into 4 sections along axis-0 :  
 [array([[ 1], 
       [ 5], 
       [ 9], 
       [13], 
       [17], 
       [21]]), array([[ 2], 
       [ 6], 
       [10], 
       [14], 
       [18], 
       [22]]), array([[ 3], 
       [ 7], 
       [11], 
       [15], 
       [19],
        [23]]), array([[ 4], 
       [ 8],
       [12], 
       [16], 
       [20], 
       [24]])]

split() based on indices

We can also split based on indices. The sizes of sub-arrays are need not be equal.

1-D arrays(axis=0)

figer banana hai

Example

Python
In [303]: 
# splitting the 1-D array based on indices 
a = np.arange(10,101,10) 
result = np.split(a,[3,7]) 
print(f"array a : {a}") 
print(f"splitting the 1-D array based on indices : \n {result}")

Output

PowerShell
array a : [ 10  20  30  40  50  60  70  80  90 100] 
splitting the 1-D array based on indices :  
 [array([10, 20, 30]), array([40, 50, 60, 70]), array([ 80,  90, 100])] 

Example

Python
In [304]: 
# splitting the 1-D array based on indices 
a = np.arange(10,101,10) 
result = np.split(a,[2,5,7])  
# [2,5,7] ==> 4 subarrays 
# subarray-1 : before index-2 ==> 0,1  
# subarray-2 : from index-2 to before index-5 ==> 2,3,4 
# subarray-3 : from index-5 to before index-7 ==> 5,6 
# subarray-4 : from index-7 to last index ==> 7,8,9 
print(f"array a : {a}") 
print(f"splitting the 1-D array based on indices : \n {result}") 

Output

PowerShell
array a : [ 10  20  30  40  50  60  70  80  90 100] 
splitting the 1-D array based on indices :  
 [array([10, 20]), array([30, 40, 50]), array([60, 70]), array([ 80,  90, 100
 ])]

figer banana hai

Example

Python
In [305]: 
# splitting 2-D arrays based on indices along axis=0 
a = np.arange(1,13).reshape(6,2) 
result = np.split(a,[3,4]) 
print(f"array a : \n {a}") 
print(f"resultant array after vertical split  : \n {result}")

Output

PowerShell
array a :  
 [[ 1  2] 
 [ 3  4] 
 [ 5  6] 
 [ 7  8] 
 [ 9 10] 
 [11 12]] 
resultant array after vertical split  :  
 [array([[1, 2], 
       [3, 4], 
       [5, 6]]), array([[7, 8]]), array([[ 9, 10], 
       [11, 12]])]

figer banana hai

Example

Python
In [306]: 
a = np.arange(1,19).reshape(3,6) 
result = np.split(a,[1,3,5],axis=1) 
print(f"array a : \n {a}") 
print(f"resultant array after horizontal split  : \n {result}")

Output

PowerShell
array a :  
 [[ 1  2  3  4  5  6] 
 [ 7  8  9 10 11 12] 
 [13 14 15 16 17 18]] 
resultant array after horizontal split  :  
 [array([[ 1], 
       [ 7], 
       [13]]), array([[ 2,  3], 
       [ 8,  9], 
       [14, 15]]), array([[ 4,  5], 
       [10, 11], 
       [16, 17]]), array([[ 6], 
       [12], 
       [18]])]

Example

Python
In [307]: 
a = np.arange(1,19).reshape(3,6) 
result = np.split(a,[2,4,4],axis=1) 
# [2,4,4] => 4 subarrays are created 
# subarray1:  2 ==> before index-2 ==> 0,1 
# subarray2:  4 ==> from index-2 to before index-4 ==> 2,3 
# subarray3:  4 ==> from index-4 to before index-4 ==> empty array 
# subarray4:    ==> from index-4 to last index ==> 4,5 
print(f"array a : \n {a}") 
print(f"resultant array after horizontal split  : \n {result}") 
print(f"first subarray : \n {result[0]}") 
print(f"second subarray : \n {result[1]}") 
print(f"third subarray : \n {result[2]}") 
print(f"fourth subarray : \n {result[3]}")

Output

Python
array a :  
 [[ 1  2  3  4  5  6] 
 [ 7  8  9 10 11 12] 
 [13 14 15 16 17 18]] 
resultant array after horizontal split  :  
 [array([[ 1,  2], 
       [ 7,  8], 
       [13, 14]]), array([[ 3,  4], 
       [ 9, 10], 
       [15, 16]]), array([], shape=(3, 0), dtype=int32), array([[ 5,  6], 
       [11, 12], 
       [17, 18]])] 
first subarray :  
 [[ 1  2] 
 [ 7  8] 
 [13 14]] 
second subarray :  
 [[ 3  4] 
 [ 9 10] 
 [15 16]] 
third subarray :  
 [] 
fourth subarray :  
 [[ 5  6] 
 [11 12] 
 [17 18]]

Example

Python
In [308]: 
a = np.arange(1,19).reshape(3,6) 
result = np.split(a,[0,2,6],axis=1) 
# [0,2,6] => 4 subarrays are created 
# subarray1:  0 ==> before index-0 ==> empty 
# subarray2:  2 ==> from index-0 to before index-2 ==> 0,1 
# subarray3:  6 ==> from index-2 to before index-6 ==> 2,3,4,5 
# subarray4:    ==> from index-6 to last index ==> empty
print(f"array a : \n {a}") 
print(f"resultant array after horizontal split  : \n {result}") 
print(f"first subarray : \n {result[0]}") 
print(f"second subarray : \n {result[1]}") 
print(f"third subarray : \n {result[2]}") 
print(f"fourth subarray : \n {result[3]}")

Output

PowerShell
array a :  
 [[ 1  2  3  4  5  6] 
 [ 7  8  9 10 11 12] 
 [13 14 15 16 17 18]] 
resultant array after horizontal split  :  
 [array([], shape=(3, 0), dtype=int32), array([[ 1,  2], 
       [ 7,  8], 
       [13, 14]]), array([[ 3,  4,  5,  6], 
       [ 9, 10, 11, 12], 
       [15, 16, 17, 18]]), array([], shape=(3, 0), dtype=int32)] 
first subarray :  
 [] 
second subarray :  
 [[ 1  2] 
 [ 7  8] 
 [13 14]] 
third subarray :  
 [[ 3  4  5  6] 
 [ 9 10 11 12] 
 [15 16 17 18]] 
fourth subarray :  
 [] 

Example

Python
In [309]: 
a = np.arange(1,19).reshape(3,6) 
result = np.split(a,[1,5,3],axis=1) 
# [1,5,3] => 4 subarrays are created 
# subarray1:  1 ==> before index-1 ==> 0 
# subarray2:  5 ==> from index-1 to before index-5 ==> 1,2,3,4 
# subarray3:  3 ==> from index-5 to before index-3 ==> empty 
# subarray4:    ==> from index-3 to last index ==> 3,4,5 
print(f"array a : \n {a}") 
print(f"resultant array after horizontal split  : \n {result}") 
print(f"first subarray : \n {result[0]}") 
print(f"second subarray : \n {result[1]}") 
print(f"third subarray : \n {result[2]}") 
print(f"fourth subarray : \n {result[3]}") 

Output

PowerShell
array a :  
 [[ 1  2  3  4  5  6] 
 [ 7  8  9 10 11 12] 
 [13 14 15 16 17 18]] 
resultant array after horizontal split  :  
 [array([[ 1], 
       [ 7], 
       [13]]), array([[ 2,  3,  4,  5], 
       [ 8,  9, 10, 11], 
       [14, 15, 16, 17]]), array([], shape=(3, 0), dtype=int32), array([[ 4,  5,  6], 
       [10, 11, 12], 
       [16, 17, 18]])] 
first subarray :  
 [[ 1] 
 [ 7] 
 [13]] 
second subarray :  
 [[ 2  3  4  5] 
 [ 8  9 10 11] 
 [14 15 16 17]] 
third subarray :  
 [] 
fourth subarray :  
 [[ 4  5  6] 
 [10 11 12] 
 [16 17 18]]

vsplit()

  • vsplit means vertical split means row wise split
  • split is based on axis-0

Example

Python
In [310]: 
import numpy as np 
help(np.vsplit) 

Output

PowerShell
Help on function vsplit in module numpy: 
 
vsplit(ary, indices_or_sections) 
    Split an array into multiple sub-arrays vertically (row-wise).

1-D arrays

  • To use vsplit, input array should be atleast 2-D array
  • It is not possible to split 1-D array vertically.

Example

Python
In [311]: 
a = np.arange(10) 
np.vsplit(a,2) 

Output

PowerShell
--------------------------------------------------------------------------- 
ValueError                                Traceback (most recent call last) 
<ipython-input-311-43d15b9a4cce> in <module> 
      1 a = np.arange(10)
     ----> 2 np.vsplit(a,2) 
 
<__array_function__ internals> in vsplit(*args, **kwargs) 
 
F:\Users\Gopi\anaconda3\lib\site-packages\numpy\lib\shape_base.py in vsplit(a
 ry, indices_or_sections) 
    988     """ 
    989     if _nx.ndim(ary) < 2: --> 990         raise ValueError('vsplit only works on arrays of 2 or more di
 mensions') 
    991     return split(ary, indices_or_sections, 0) 
    992  
 
ValueError: vsplit only works on arrays of 2 or more dimensions

2-D arrays

Python
In [312]: 
a = np.arange(1,13).reshape(6,2) 
np.vsplit(a,2)

Output

PowerShell
Out[312]: 
[array([[1, 2], 
        [3, 4], 
        [5, 6]]), 
 array([[ 7,  8], 
        [ 9, 10], 
        [11, 12]])]

Example

Python
In [313]: 
np.vsplit(a,3) 

Output

PowerShell
Out[313]: 
[array([[1, 2], 
        [3, 4]]), 
 array([[5, 6], 
        [7, 8]]), 
 array([[ 9, 10], 
        [11, 12]])]

Example

Python
In [314]: 
np.vsplit(a,6)

Output

PowerShell
Out[314]: 
[array([[1, 2]]), 
 array([[3, 4]]), 
 array([[5, 6]]), 
 array([[7, 8]]), 
 array([[ 9, 10]]), 
 array([[11, 12]])] 

Example

Python
In [315]: 
# vsplit() based on indices: 
a = np.arange(1,13).reshape(6,2) 
np.vsplit(a,[3,4])

Output

PowerShell
Out[315]: 
[array([[1, 2], 
        [3, 4], 
        [5, 6]]), 
 array([[7, 8]]), 
 array([[ 9, 10], 
        [11, 12]])] 

hsplit()

  • hsplit—>means horizontal split(column wise)
  • split will be happend based on 2nd axis (axis-1)

Example

Python
In [316]: 
import numpy as np 
help(np.hsplit)

Output

PowerShell
Help on function hsplit in module numpy: 
 
hsplit(ary, indices_or_sections) 
    Split an array into multiple sub-arrays horizontally (column-wise).

1-D arrays

Example

Python
In [317]: 
a = np.arange(10) 
np.hsplit(a,2) 

Output

PowerShell
Out[317]: 
[array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]

2-D arrays

  • Based on axis-1 only

Example

Python
In [318]: 
a = np.arange(1,13).reshape(3,4) 
np.hsplit(a,2)

Output

PowerShell
Out[318]: 
[array([[ 1,  2], 
        [ 5,  6], 
        [ 9, 10]]), 
 array([[ 3,  4], 
        [ 7,  8], 
        [11, 12]])] 

Example

Python
In [319]: 
# hsplit() based on indices: 
a = np.arange(10,101,10) 
np.hsplit(a,[2,4])

Output

PowerShell
Out[319]: 
[array([10, 20]), array([30, 40]), array([ 50,  60,  70,  80,  90, 100])] 

Example

Python
In [320]: 
a = np.arange(24).reshape(4,6) 
np.hsplit(a,[2,4]) 

Output

PowerShell
Out[320]: 
[array([[ 0,  1], 
        [ 6,  7], 
        [12, 13], 
        [18, 19]]), 
 array([[ 2,  3], 
        [ 8,  9], 
        [14, 15], 
        [20, 21]]), 
 array([[ 4,  5], 
        [10, 11], 
        [16, 17], 
        [22, 23]])] 

dsplit()

  • dsplit —>means depth split
  • splitting based on 3rd axis(axis-2).

Example

Python
In [321]: 
import numpy as np 
help(np.dsplit) 

Output

PowerShell
Help on function dsplit in module numpy: 
 
dsplit(ary, indices_or_sections) 
    Split array into multiple sub-arrays along the 3rd axis (depth). 
     
Syntax: 
 dsplit(array, indices_or_sections) 
    -->Split array into multiple sub-arrays along the 3rd axis (depth). 
    -->'dsplit' is equivalent to 'split' with 'axis=2', the array is always 
  split along the third axis provided the array dimension is greater than or eq
 ual to 3  

Example

Python
In [322]: 
a = np.arange(24).reshape(2,3,4) 
a 

Output

PowerShell
Out[322]: 
array([[[ 0,  1,  2,  3], 
        [ 4,  5,  6,  7], 
        [ 8,  9, 10, 11]], 
 
       [[12, 13, 14, 15], 
        [16, 17, 18, 19], 
        [20, 21, 22, 23]]])

Example

Python
In [323]: 
# dsplit based on sections 
np.dsplit(a,2) 

Output

PowerShell
Out[323]: 
[array([[[ 0,  1], 
         [ 4,  5], 
         [ 8,  9]], 
  
        [[12, 13], 
         [16, 17], 
         [20, 21]]]), 
 array([[[ 2,  3], 
         [ 6,  7], 
         [10, 11]], 
  
        [[14, 15], 
         [18, 19], 
         [22, 23]]])] 

Example

Python
In [324]: 
# dsplit based on indices 
np.dsplit(a,[1,3]) 

Output

PowerShell
Out[324]: 
[array([[[ 0], 
         [ 4], 
         [ 8]], 
  
        [[12], 
         [16], 
         [20]]]), 
 array([[[ 1,  2], 
         [ 5,  6], 
         [ 9, 10]], 
  
        [[13, 14], 
         [17, 18], 
         [21, 22]]]), 
 array([[[ 3], 
         [ 7], 
         [11]], 
  
        [[15], 
         [19], 
         [23]]])]

array_split()

  • In the case of split() with sections the array should be splitted into equal parts. if equal parts are not possible, then we will get error.
  • But in the case of array_split() we won’t get any error.

Example

Python
In [325]: 
import numpy as np 
help(np.array_split) 

Output

PowerShell
Help on function array_split in module numpy: 
 
array_split(ary, indices_or_sections, axis=0) 
    Split an array into multiple sub-arrays. 
    
  • The only difference between split() and array_split() is that ‘array_split’ allows ‘indices_or_ sections’ to be an integer that does not equally divide the axis.
  • For an array of length x that should be split into n sections, it returns x % n sub arrays of size x//n + 1 and the rest of size x//n

Example

Python
In [326]: 
#  x % n sub-arrays of size x//n + 1  
#  and the rest of size x//n 
# eg-1: 
# 10 elements --->3 sections 
# 10%3(1) sub-arrays of size 10//3+1(4) 
# and the rest(2) of size 10//3 (3) 
# 1 sub-array of size 4 and the rest of size 3 
#(4,3,3) 
a = np.arange(10,101,10) 
np.array_split(a,3) 

Output

PowerShell
Out[326]: 
[array([10, 20, 30, 40]), array([50, 60, 70]), array([ 80,  90, 100])] 

Example

Python
In [327]: 
# Eg: 2 
# 11 elements    3 sections 
 
# it returns x % n (11%3=2)sub-arrays of size x//n + 1(11//3+1=4) 
# and the rest(1) of size x//n.(11//3=3) 
# (4,4,3) 
a = np.arange(11) 
np.array_split(a,3)

Output

PowerShell
Out[327]: 
[array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8,  9, 10])]

Example

Python
In [328]: 
# 2-D array 
# x=6 n=4 
# x % n sub-arrays of size x//n + 1-->2 sub-arrays of size:2  
# rest of size x//n.--->2 sub-arrays of size:1  
# 2,2,1,1, 
a = np.arange(24).reshape(6,4) 
a 

Output

PowerShell
Out[328]: 
array([[ 0,  1,  2,  3], 
       [ 4,  5,  6,  7], 
       [ 8,  9, 10, 11], 
       [12, 13, 14, 15], 
       [16, 17, 18, 19], 
       [20, 21, 22, 23]])

Example

Python
In [329]: 
np.array_split(a,4)

Output

PowerShell
Out[329]: 
[array([[0, 1, 2, 3], 
        [4, 5, 6, 7]]), 
 array([[ 8,  9, 10, 11], 
        [12, 13, 14, 15]]), 
 array([[16, 17, 18, 19]]), 
 array([[20, 21, 22, 23]])] 

Summary of split methods:

  • split() ==>Split an array into multiple sub-arrays of equal size. Raise error if an equal division is cannot be made
  • vsplit() ==>Split array into multiple sub-arrays vertically (row wise).
  • hsplit() ==>Split array into multiple sub-arrays horizontally (column-wise).
  • dsplit() ==> Split array into multiple sub-arrays along the 3rd axis (depth).
  • array_split() ==>Split an array into multiple sub-arrays of equal or near-equal size.Does not raise an exception if an equal division cannot be made.

Report an error