w3resource

Divide each 2D slice of 3D array by 1D array using np.divide

NumPy: Universal Functions Exercise-13 with Solution

ufunc with Broadcasting:

Write a NumPy program that creates a 3D array and a 1D array and uses np.divide to divide each 2D slice of the 3D array by the 1D array.

Sample Solution:

Python Code:

import numpy as np

# Create a 3D NumPy array of shape (3, 4, 5) with random integers
array_3d = np.random.randint(1, 10, size=(3, 4, 5))

# Create a 1D NumPy array with random integers
array_1d = np.random.randint(1, 10, size=5)

# Use the np.divide ufunc to divide each 2D slice of the 3D array by the 1D array
result_array = np.divide(array_3d, array_1d)

# Print the original arrays and the resulting array
print('Original 3D array:\n', array_3d)
print('1D array:\n', array_1d)
print('Resulting array after dividing each 2D slice by the 1D array:\n', result_array)

Output:

Original 3D array:
 [[[1 3 6 2 6]
  [9 8 8 3 8]
  [9 5 7 6 9]
  [5 2 7 6 9]]

 [[4 6 3 9 7]
  [2 9 4 8 7]
  [3 4 3 9 9]
  [2 4 4 1 6]]

 [[2 6 5 2 3]
  [9 5 7 6 6]
  [6 2 7 6 7]
  [3 6 2 9 8]]]
1D array:
 [7 3 2 4 3]
Resulting array after dividing each 2D slice by the 1D array:
 [[[0.14285714 1.         3.         0.5        2.        ]
  [1.28571429 2.66666667 4.         0.75       2.66666667]
  [1.28571429 1.66666667 3.5        1.5        3.        ]
  [0.71428571 0.66666667 3.5        1.5        3.        ]]

 [[0.57142857 2.         1.5        2.25       2.33333333]
  [0.28571429 3.         2.         2.         2.33333333]
  [0.42857143 1.33333333 1.5        2.25       3.        ]
  [0.28571429 1.33333333 2.         0.25       2.        ]]

 [[0.28571429 2.         2.5        0.5        1.        ]
  [1.28571429 1.66666667 3.5        1.5        2.        ]
  [0.85714286 0.66666667 3.5        1.5        2.33333333]
  [0.42857143 2.         1.         2.25       2.66666667]]]

Explanation:

  • Import Libraries:
    • Imported numpy as "np" for array creation and manipulation.
  • Create 3D NumPy Array:
    • Create a 3D NumPy array named 'array_3d' with random integers ranging from 1 to 9 and a shape of (3, 4, 5).
  • Create 1D NumPy Array:
    • Create a 1D NumPy array named 'array_1d' with random integers ranging from 1 to 9 and a length of 5.
  • Broadcasting with np.divide:
    • Used the np.divide "ufunc" to divide each 2D slice of the 3D array by the 1D array. NumPy automatically broadcasts the 1D array to match the shape of each 2D slice.
  • Finally print the original 3D array, the 1D array, and the resulting array after division.

Python-Numpy Code Editor:

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

Previous: Compute Dot Product using np.dot and custom ufunc in NumPy.
Next: How to define and apply a Custom ufunc with Broadcasting in NumPy.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/divide-each-2d-slice-of-3d-array-by-1d-array-using-np-dot-divide.php