Select Subset of elements using combined Indexing in NumPy
NumPy: Advanced Indexing Exercise-12 with Solution
Combined Indexing:
Write a NumPy program that creates a 2D NumPy array and uses a combination of boolean and integer indexing to select a subset of elements.
Sample Solution:
Python Code:
import numpy as np
# Create a 2D NumPy array of shape (5, 5) with random integers
array_2d = np.random.randint(0, 100, size=(5, 5))
# Define the boolean condition to select elements greater than 50
condition = array_2d > 50
# Define the specific rows to apply the condition
row_indices = np.array([1, 3, 4])
# Use a combination of boolean and integer indexing to select elements
selected_elements = array_2d[row_indices][:, condition[row_indices][0]]
# Print the original array and the selected elements
print('Original 2D array:\n', array_2d)
print('Condition (elements > 50):\n', condition)
print('Row indices:\n', row_indices)
print('Selected elements using combined indexing:\n', selected_elements)
Output:
Original 3D array: [[[ 5 97 52 61 57] [79 87 75 83 21] [52 1 33 54 10] [76 58 44 0 72]] [[40 7 30 18 61] [24 1 9 98 25] [77 75 3 82 5] [90 63 59 79 52]] [[49 69 60 80 28] [45 60 63 31 69] [18 49 62 25 87] [85 94 35 9 8]]] Depth indices: [0 1 2] Row indices: [1 2 3] Column indices: [2 3 4] Selected elements: [[[75 83 21] [33 54 10] [44 0 72]] [[ 9 98 25] [ 3 82 5] [59 79 52]] [[63 31 69] [62 25 87] [35 9 8]]] runfile('C:/Users/ME/untitled1.py', wdir='C:/Users/ME') Original 2D array: [[69 78 3 8 84] [93 66 89 88 5] [55 29 47 79 33] [ 4 5 73 6 28] [27 18 66 70 61]] Condition (elements > 50): [[ True True False False True] [ True True True True False] [ True False False True False] [False False True False False] [False False True True True]] Row indices: [1 3 4] Selected elements using combined indexing: [[93 66 89 88] [ 4 5 73 6] [27 18 66 70]]
Explanation:
- Import Libraries:
- Imported numpy as "np" for array creation and manipulation.
- Create 2D NumPy Array:
- Create a 2D NumPy array named array_2d with random integers ranging from 0 to 99 and a shape of (5, 5).
- Define Boolean Condition:
- Define a boolean condition to select elements in the array that are greater than 50.
- Define Specific Rows:
- Define row_indices to specify the rows to which the condition will be applied.
- Combined Indexing:
- Used a combination of boolean and integer indexing to select elements from the specified rows that meet the condition.
- Print Results:
- Print the original 2D array, the boolean condition array, the row indices, and the selected elements to verify the indexing operation.
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Select elements from 3D NumPy array using integer Indexing.
Next: Select elements using Boolean Indexing with logical operators.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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/select-subset-of-elements-using-combined-indexing-in-numpy.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics