Using numpy.dsplit() function (3 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

The numpy.dsplit() function is a powerful tool for splitting arrays along the third dimension, enabling efficient data manipulation and analysis. In this article, we delve into how to use this function effectively, with examples ranging from basic to advanced applications.

Syntax & Parameters

NumPy’s dsplit function is essential for working with 3D arrays or tensors, facilitating the splitting of these data structures along their depth. Understanding its usage can significantly enhance data manipulation capabilities within your numerical computing projects.

Syntax:

numpy.dsplit(ary, indices_or_sections)

Parameters:

  • ary: array_like. An array to be divided into sub-arrays. Must be at least 3-D.
  • indices_or_sections: int or 1-D array_like. If an integer, N, the array will be divided into N equal arrays along the third axis. If such a split is not possible, an error will be raised. If an array, the elements represent the points at which to split the array along the third axis. If an index exceeds the dimension of the array along the third axis, an empty sub-array is returned correspondingly.

Returns:

  • sub_arrays: list of ndarrays. A list of sub-arrays.

Example 1: Basic Splitting

Let’s start with the basics by splitting a simple 3D array into equal parts.

import numpy as np

np.random.seed(2024)

# Creating a 3D array
array = np.random.rand(3, 4, 6)

# Splitting the array into 3 parts
parts = np.dsplit(array, 3)

for i, part in enumerate(parts):
  print(f"Part {i+1}:\n", part)

Output:

Part 1:
 [[[0.58801452 0.69910875]
  [0.72724014 0.67940052]
  [0.60244854 0.96177758]
  [0.6701743  0.73576659]]

 [[0.28216512 0.76825393]
  [0.28582739 0.74026815]
  [0.78450686 0.75895366]
  [0.59643269 0.83732372]]

 [[0.25592093 0.86723234]
  [0.24594844 0.06401838]
  [0.34680397 0.31287816]
  [0.55921377 0.69451294]]]
Part 2:
 [[[0.18815196 0.04380856]
  [0.4738457  0.44829582]
  [0.66436865 0.60662962]
  [0.25799564 0.09554215]]

 [[0.7979234  0.5440372 ]
  [0.23898683 0.4377217 ]
  [0.41778538 0.22576877]
  [0.89248639 0.20052744]]

 [[0.01648793 0.55249695]
  [0.9021047  0.8740398 ]
  [0.84710402 0.8802311 ]
  [0.8241973  0.31142866]]]
Part 3:
 [[[0.20501895 0.10606287]
  [0.01910695 0.75259834]
  [0.44915131 0.22535416]
  [0.96090974 0.25176729]]

 [[0.38270763 0.38165095]
  [0.8835387  0.28928114]
  [0.42009814 0.06436369]
  [0.50239523 0.89538184]]

 [[0.52790539 0.92335039]
  [0.16366729 0.99974131]
  [0.67655865 0.05367515]
  [0.50523054 0.84900379]]]

In this example, we’ve divided a 3D array of dimensions (3, 4, 6) into three parts along its depth, each of which retains its original shape in the other dimensions (3, 4, 2).

Example 2: Splitting with Indices

Beyond equally dividing arrays, dsplit allows for split at specified indices, offering flexibility in handling data.

import numpy as np

np.random.seed(2024)

# Creating another 3D array
array = np.random.rand(3, 4, 9)

# Splitting at specified indices
indices = [3, 5]
parts = np.dsplit(array, indices)

for i, part in enumerate(parts):
    print(f"Part {i+1}:\n", part)

Output:

Part 1:
 [[[0.58801452 0.69910875 0.18815196]
  [0.44829582 0.01910695 0.75259834]
  [0.6701743  0.73576659 0.25799564]
  [0.5440372  0.38270763 0.38165095]]

 [[0.78450686 0.75895366 0.41778538]
  [0.20052744 0.50239523 0.89538184]
  [0.24594844 0.06401838 0.9021047 ]
  [0.8802311  0.67655865 0.05367515]]

 [[0.29351563 0.67711955 0.4209064 ]
  [0.37966499 0.78752081 0.16886931]
  [0.39039811 0.63561732 0.83114886]
  [0.17323332 0.53173259 0.87095862]]]
Part 2:
 [[[0.04380856 0.20501895]
  [0.60244854 0.96177758]
  [0.09554215 0.96090974]
  [0.28582739 0.74026815]]

 [[0.22576877 0.42009814]
  [0.25592093 0.86723234]
  [0.8740398  0.16366729]
  [0.55921377 0.69451294]]

 [[0.68171271 0.22122799]
  [0.58635861 0.43121067]
  [0.319421   0.15922479]
  [0.84109027 0.97205554]]]
Part 3:
 [[[0.10606287 0.72724014 0.67940052 0.4738457 ]
  [0.66436865 0.60662962 0.44915131 0.22535416]
  [0.25176729 0.28216512 0.76825393 0.7979234 ]
  [0.23898683 0.4377217  0.8835387  0.28928114]]

 [[0.06436369 0.59643269 0.83732372 0.89248639]
  [0.01648793 0.55249695 0.52790539 0.92335039]
  [0.99974131 0.34680397 0.31287816 0.84710402]
  [0.8241973  0.31142866 0.50523054 0.84900379]]

 [[0.5489977  0.84884672 0.7365669  0.49962259]
  [0.06191019 0.28945477 0.7341454  0.28865545]
  [0.71166422 0.87270864 0.59315637 0.69471288]
  [0.78225721 0.19703051 0.61062607 0.47885551]]]

This approach yields portions of varying dimensions based on the specified indices, enabling targeted analysis or manipulation of subsets within the larger dataset.

Example 3: Advanced Application – Processing Split Data

Combining dsplit with other NumPy functionalities opens up advanced data processing avenues. Here, we demonstrate using split parts in computations.

import numpy as np

np.random.seed(2024)

# Creating a complex 3D array
array = np.random.rand(3, 4, 10)

# Splitting into two parts
parts = np.dsplit(array, 2)

# Example computation on the first part
mean_first_part = np.mean(parts[0], axis=2)
print("Mean of first part along the depth:", mean_first_part)

Output:

Mean of first part along the depth: [[0.34482055 0.60006001 0.36967599 0.51726856]
 [0.56214093 0.45323782 0.61271518 0.54915518]
 [0.48440726 0.46716202 0.79961885 0.63433066]]

This example illustrates how data split using dsplit can be directly utilized in subsequent analyses, showcasing the function’s utility in complex workflows.

Conclusion

Through these examples, it is evident that NumPy’s dsplit function is an indispensable tool for sophisticated array manipulation, especially with 3D data. Its application ranges from simple dataset partitioning to enabling intricate analyses by subset. Mastering dsplit can significantly amplify your data processing capacities.