How do you use NumPy advanced indexing to select elements of a multidimensional array by condition or index arrays?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
In NumPy, how do you perform advanced indexing to select elements from a multidimensional array based on a condition or an array of indices? For example, given a 2D array you might want every element above a threshold, a list of specific (row, column) positions, or a set of whole rows.
```hint Pairing or crossing
When you pass one index array per axis, work out whether NumPy pairs the arrays entry by entry or forms every combination of them, and check it on a small array.
```
### What a Strong Answer Covers
- How integer index arrays select elements across several axes, and how their shapes combine into the result's shape.
- How boolean masks select individual elements or whole rows and columns, and the shape of what comes back.
- How to recover the coordinates of the elements that match a condition.
- Copy versus view behavior, and what happens when you assign through an advanced index.
- Common pitfalls, such as selecting a submatrix or combining several conditions.
### Follow-up Questions
- How would you select the submatrix formed by rows 0 and 2 and columns 1 and 3?
- What does `x[[0, 0, 1]] += 1` do, and how would you make repeated indices accumulate?
- How do you replace negative values with zero while keeping the array's shape?
- For an array `c` of shape (5, 6, 7), what is the shape of `c[[0, 1], :, [0, 1]]`, and why?
Overview: Asks how to use NumPy advanced indexing to pull elements out of a multidimensional array with a boolean condition or with arrays of indices. It tests how index arrays pair up and broadcast, the shape of the result, copy versus view behavior, and common indexing pitfalls.