0
0
NumPydata~5 mins

Fancy indexing with integer arrays in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly slices
BStrings
CInteger arrays or lists
DBoolean values
What does arr[[0, 2, 4]] do if arr = np.array([5, 10, 15, 20, 25])?
ARaises an error
BReturns <code>[10, 20]</code>
CReturns <code>[5, 10, 15, 20, 25]</code>
DReturns <code>[5, 15, 25]</code>
Can fancy indexing select elements in any order?
AYes, order can be arbitrary
BNo, only ascending order
CNo, only descending order
DOnly continuous ranges
What happens if you assign values using fancy indexing with repeated indices?
AAll assignments are summed
BThe last assignment to a repeated index is kept
CAn error occurs
DValues are averaged
Which of these is NOT a use case for fancy indexing?
ASelecting a continuous slice of elements
BDuplicating elements in a new array
CReordering elements arbitrarily
DSelecting specific rows from a 2D array
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.