w3resource

NumPy Advanced Indexing: Exercises, Practice, and Solutions


This resource offers a total of 100 NumPy Advanced Indexing problems for practice. It includes 20 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

This document provides a series of exercises focusing on advanced NumPy indexing techniques, including boolean, integer, and fancy indexing across multi-dimensional arrays (2D to 5D).

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. 2D Array Random Integers & Boolean Indexing

Write a NumPy program that creates a 2D NumPy array of random integers. Use boolean indexing to select all elements greater than a specified value.

Click me to see the sample solution


2. 1D Array & Integer Array Indexing

Write a NumPy program that creates a 1D NumPy array and uses integer array indexing to select a subset of elements based on an index array.

Click me to see the sample solution


3. 3D Array & Fancy Indexing

Write a NumPy program that creates a 3D NumPy array and uses fancy indexing to select elements from specific rows and columns.

Click me to see the sample solution


4. 4D Array & Multi-dimensional Indexing

Write a NumPy program that creates a 4D NumPy array and uses multi-dimensional indexing to select a subarray.

Click me to see the sample solution


5. 2D Random Floats & Multiple Condition Boolean Indexing

Write a NumPy program that creates a 2D NumPy array of random floats and uses boolean indexing to select elements that satisfy multiple conditions (e.g., greater than 0.5 and less than 0.8).

Click me to see the sample solution


6. 2D Array & Integer Indexing with Broadcasting

Write a NumPy program that creates a 2D NumPy array and uses integer indexing with broadcasting to select elements from specific rows and all columns.

Click me to see the sample solution


7. 3D Array & Boolean Indexing Along One Axis

Write a NumPy program that creates a 3D NumPy array and use boolean indexing to select elements along one axis based on conditions applied to another axis.

Click me to see the sample solution


8. 2D Array & Tuple of Arrays Indexing

Write a NumPy program that creates a 2D NumPy array and uses a tuple of arrays to index and select a specific set of elements.

Click me to see the sample solution


9. Cross-Indexing with np.ix_

Write a NumPy program that creates two 1D NumPy arrays and uses np.ix_ to perform cross-indexing on a 2D array.

Click me to see the sample solution


10. 2D Array & Boolean Indexing Replacement

Write a NumPy program that creates a 2D NumPy array and uses boolean indexing to replace all elements that meet a certain condition with a specified value.

Click me to see the sample solution


11. 3D Array & Integer Array Indexing Along Specific Axes

Write a NumPy program that creates a 3D NumPy array and uses integer array indexing to select elements along specific axes.

Click me to see the sample solution


12. 2D Array & Combined Boolean and Integer 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.

Click me to see the sample solution


13. 1D Array & Boolean Indexing with Logical Operators

Write a NumPy program that creates a 1D NumPy array and uses boolean indexing with logical operators (e.g., & for AND, | for OR) to select elements based on multiple conditions.

Click me to see the sample solution


14. 2D Array Slicing with Index Arrays for Subarray

Write a NumPy program that creates a 2D NumPy array and uses slicing in combination with index arrays to select a rectangular subarray.

Click me to see the sample solution


15. 3D Array & Boolean Indexing for Value Replacement

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.

Click me to see the sample solution


16. 2D Array & np.nonzero Advanced Indexing

Write a NumPy program that creates a 2D NumPy array and uses np.nonzero to find indices of elements that satisfy a condition, then uses these indices for advanced indexing.

Click me to see the sample solution


17. 4D Array & Integer Indexing to Extract Subarrays

Write a NumPy program that creates a 4D NumPy array and uses integer indexing to extract smaller subarrays.

Click me to see the sample solution


18. 3D Array & Combined Slicing and Integer Indexing

Write a Numpy program that creates a 3D NumPy array and use a combination of slicing and integer indexing to select a specific slice and then index into it.

Click me to see the sample solution


19. 5D Array & Boolean Indexing Across 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.

Click me to see the sample solution


20. 2D Array & Mask Array for Subset Selection

Write a NumPy program that creates a 2D NumPy array and uses a mask array (boolean array) for indexing to select a subset of elements that match the mask criteria.

Click me to see the sample solution


Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.