w3resource

Set elements in 3D NumPy array using Boolean Indexing

NumPy: Advanced Indexing Exercise-15 with Solution

Setting Values with Boolean Indexing:

Write a NumPy program that creates a 3D NumPy array and uses boolean indexing to set elements that meet a condition to a new value.

Sample Solution:

Python Code:

import numpy as np

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

# Define the condition to set elements less than 50 to -1
condition = array_3d < 50

# Use boolean indexing to set elements that meet the condition to -1
array_3d[condition] = -1

# Print the original array and the modified array
print('Original 3D array:\n', array_3d)
print('Condition (elements < 50):\n', condition)
print('Modified 3D array with elements < 50 set to -1:\n', array_3d)

Output:

Original 3D array:
 [[[-1 -1 -1 -1]
  [-1 -1 -1 70]
  [-1 69 85 60]
  [81 63 -1 -1]]

 [[-1 61 61 82]
  [81 59 -1 74]
  [-1 -1 77 67]
  [80 99 -1 -1]]

 [[-1 -1 -1 66]
  [-1 58 -1 -1]
  [53 -1 75 -1]
  [61 65 92 -1]]

 [[84 94 71 -1]
  [59 85 -1 -1]
  [52 77 54 95]
  [-1 -1 -1 74]]]
Condition (elements < 50):
 [[[ True  True  True  True]
  [ True  True  True False]
  [ True False False False]
  [False False  True  True]]

 [[ True False False False]
  [False False  True False]
  [ True  True False False]
  [False False  True  True]]

 [[ True  True  True False]
  [ True False  True  True]
  [False  True False  True]
  [False False False  True]]

 [[False False False  True]
  [False False  True  True]
  [False False False False]
  [ True  True  True False]]]
Modified 3D array with elements < 50 set to -1:
 [[[-1 -1 -1 -1]
  [-1 -1 -1 70]
  [-1 69 85 60]
  [81 63 -1 -1]]

 [[-1 61 61 82]
  [81 59 -1 74]
  [-1 -1 77 67]
  [80 99 -1 -1]]

 [[-1 -1 -1 66]
  [-1 58 -1 -1]
  [53 -1 75 -1]
  [61 65 92 -1]]

 [[84 94 71 -1]
  [59 85 -1 -1]
  [52 77 54 95]
  [-1 -1 -1 74]]]

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 0 to 99 and a shape of (4, 4, 4).
  • Define Condition:
    • Define a condition to select elements in the array that are less than 50 using boolean indexing.
  • Boolean Indexing to Set Values:
    • Applied boolean indexing to set elements in array_3d that meet the condition (elements less than 50) to -1.
  • Print Results:
    • Print the original 3D array, the boolean condition array, and the modified 3D array to verify the operation.

Python-Numpy Code Editor:

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

Previous: Select Rectangular Subarray using advanced slicing in NumPy.
Next: Find and Index elements in 2D NumPy array using np.nonzero.

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://w3resource.com/python-exercises/numpy/set-elements-in-3d-numpy-array-using-boolean-Indexing.php