w3resource

NumPy Array manipulation: dsplit() function

numpy.dsplit() function

The dsplit() function is used to split an array into multiple sub-arrays.
The only difference between these functions is that dsplit allows indices_or_sections to be an integer that does not equally divide the axis. For an array of length l that should be split into n sections, it returns l % n sub-arrays of size l//n + 1 and the rest of size l//n.

Syntax:

numpy.dsplit(ary, indices_or_sections, axis=0)
NumPy manipulation: dsplit() function

Version: 1.15.0

Example-1: numpy.dsplit()

>>> import numpy as np
>>> a = np.arange(12.0).reshape (2,2,3)
>>> a
array([[[  0.,   1.,   2.],
        [  3.,   4.,   5.]],

       [[  6.,   7.,   8.],
        [  9.,  10.,  11.]]])

Pictorial Presentation:

NumPy manipulation: dsplit() function

Example-2: numpy.dsplit()

>>> import numpy as np
>>> a = np.arange(12.0).reshape (2,2,3)
	
>>> np.dsplit(a,3)
[array([[[ 0.],
        [ 3.]],

       [[ 6.],
        [ 9.]]]), array([[[  1.],
        [  4.]],

       [[  7.],
        [ 10.]]]), array([[[  2.],
        [  5.]],

       [[  8.],
        [ 11.]]])]
>>> np.dsplit(a, np.array([2,6]))
[array([[[  0.,   1.],
        [  3.,   4.]],

       [[  6.,   7.],
        [  9.,  10.]]]), array([[[  2.],
        [  5.]],

       [[  8.],
        [ 11.]]]), array([], shape=(2, 2, 0), dtype=float64)]

Python - NumPy Code Editor:

Previous: array_split()
Next: hsplit()