w3resource

NumPy: Split array into multiple sub-arrays along the 3rd axis

NumPy: Array Object Exercise-132 with Solution

Write a NumPy program to split array into multiple sub-arrays along the 3rd axis.

Sample Solution:

Python Code:

import numpy as np    
print("\nOriginal arrays:")
x = np.arange(16.0).reshape(2, 2, 4)
print(x)
new_array1 = np.dsplit(x, 2)
print("\nsplit array into multiple sub-arrays along the 3rd axis:")
print(new_array1)

Sample Output:

Original arrays:
[[[ 0.  1.  2.  3.]
  [ 4.  5.  6.  7.]]

 [[ 8.  9. 10. 11.]
  [12. 13. 14. 15.]]]

split array into multiple sub-arrays along the 3rd axis:
[array([[[ 0.,  1.],
        [ 4.,  5.]],

       [[ 8.,  9.],
        [12., 13.]]]), array([[[ 2.,  3.],
        [ 6.,  7.]],

       [[10., 11.],
        [14., 15.]]])]

Pictorial Presentation:

Python NumPy: Split array into multiple sub-arrays along the 3rd axis.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to split an given array into multiple sub-arrays vertically (row-wise).
Next: Write a NumPy program to count the number of dimensions, number of elements and number of bytes for each element in a given array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}