w3resource

Boolean Indexing on higher dimensions in NumPy arrays

NumPy: Advanced Indexing Exercise-19 with Solution

Boolean Indexing on Higher Dimensions:

Write a NumPy program that creates a 5D NumPy array. Use boolean indexing to select elements along specific dimensions based on conditions applied to other dimensions.

Sample Solution:

Python Code:

import numpy as np

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

# Define a condition on the entire 5D array
# For example, select elements where the values are greater than 50
condition = array_5d > 50

# Use boolean indexing to select elements along specific dimensions based on the condition
selected_elements = array_5d[condition]

# Print the shape of the original array, the condition array, and the selected elements
print('Original 5D array shape:', array_5d.shape)
print('Condition (elements > 50):\n', condition)
print('Selected elements based on condition:\n', selected_elements)
print('Shape of selected elements:', selected_elements.shape)

Output:

Original 5D array shape: (3, 4, 2, 3, 5)
Condition (elements > 50):
 [[[[[False  True False False False]
    [ True  True False  True False]
    [False  True False  True  True]]

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


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

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


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

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


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

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



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

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


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

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


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

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


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

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



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

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


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

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


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

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


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

   [[ True  True  True  True  True]
    [ True False False  True  True]
    [False  True  True False False]]]]]
Selected elements based on condition:
 [99 71 55 88 81 74 69 58 71 51 97 73 69 77 62 68 53 72 67 99 91 51 61 53
 68 67 58 51 83 96 78 72 98 60 58 73 67 90 63 52 67 90 64 98 97 73 58 84
 68 55 84 73 66 55 62 78 84 62 53 76 73 95 96 63 79 84 59 64 74 82 71 96
 57 87 57 98 54 80 93 76 66 55 79 80 58 72 91 59 89 81 55 60 94 87 53 69
 52 53 95 99 94 99 74 58 92 81 99 56 70 72 51 54 51 76 51 82 96 77 55 88
 77 94 78 79 99 93 85 87 80 66 85 54 71 97 71 86 96 72 67 87 97 93 83 74
 69 92 96 68 92 98 72 94 56 99 93 68 56 97 95 98 73 88 71 52 74 78 99 76
 71 90 53 54 90 82 67]
Shape of selected elements: (175,)

Explanation:

  • Import Libraries:
    • Imported numpy as "np" for array creation and manipulation.
  • Create 5D NumPy Array:
    • Create a 5D NumPy array named array_5d with random integers ranging from 0 to 99 and a shape of (3, 4, 2, 3, 5).
  • Define Condition:
    • Define a condition to select elements in the array that are greater than 50 using boolean indexing.
  • Boolean Indexing:
    • Applied boolean indexing to select elements from array_5d that meet the defined condition.
  • Print Results:
    • Print the shape of the original 5D array, the boolean condition array, and the selected elements, including the shape of the selected elements to verify the operation.

Python-Numpy Code Editor:

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

Previous: Combine slicing and Indexing in NumPy to select elements.
Next: Select elements using Mask Indexing in 2D NumPy arrays

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/boolean-indexing-on-higher-dimensions-in-numpy-arrays.php