Recall & Review
beginner
What is fancy indexing in NumPy?
Fancy indexing is a way to select elements from a NumPy array using integer arrays or lists as indices, allowing you to pick multiple elements at once in any order.
Click to reveal answer
beginner
How does fancy indexing differ from slicing in NumPy?
Slicing selects a continuous range of elements, while fancy indexing lets you select arbitrary elements using arrays of indices, which can be non-continuous and repeated.
Click to reveal answer
beginner
Given array
arr = np.array([10, 20, 30, 40, 50]), what does arr[[1, 3, 4]] return?It returns a new array with elements at positions 1, 3, and 4:
[20, 40, 50].Click to reveal answer
intermediate
Can fancy indexing be used to modify elements in a NumPy array? How?
Yes. You can assign new values to elements selected by fancy indexing. For example,
arr[[1, 3]] = [100, 200] changes elements at positions 1 and 3.Click to reveal answer
intermediate
What happens if you use repeated indices in fancy indexing, like
arr[[2, 2, 0]]?The output array will include repeated elements corresponding to the repeated indices. For example, it will return elements at positions 2, 2, and 0, repeating values accordingly.
Click to reveal answer
What type of object can be used for fancy indexing in NumPy?
✗ Incorrect
Fancy indexing uses integer arrays or lists to select elements by their positions.
What does
arr[[0, 2, 4]] do if arr = np.array([5, 10, 15, 20, 25])?✗ Incorrect
It selects elements at indices 0, 2, and 4.
Can fancy indexing select elements in any order?
✗ Incorrect
Fancy indexing allows arbitrary order and repetition of indices.
What happens if you assign values using fancy indexing with repeated indices?
✗ Incorrect
When assigning, repeated indices get overwritten in order, so the last value sticks.
Which of these is NOT a use case for fancy indexing?
✗ Incorrect
Continuous slices are done with slicing, not fancy indexing.
Explain fancy indexing with integer arrays in NumPy and give a simple example.
Think about how you pick specific items from a list using their positions.
You got /3 concepts.
Describe how you can modify elements in a NumPy array using fancy indexing.
Consider how you change values at certain positions in a list.
You got /3 concepts.