0
0
NumPydata~5 mins

Combining fancy and slice indexing in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is fancy indexing in numpy?
Fancy indexing means selecting elements from an array using another array of indices. It allows picking specific elements in any order.
Click to reveal answer
beginner
How does slice indexing differ from fancy indexing?
Slice indexing selects a continuous range of elements using start:stop:step syntax, while fancy indexing selects elements at specific positions given by an array of indices.
Click to reveal answer
intermediate
What happens when you combine fancy and slice indexing in numpy?
You can select specific rows or columns using fancy indexing and then select a slice of elements within those rows or columns. This lets you pick complex subsets of data.
Click to reveal answer
beginner
Example: Given a 2D array, how to select rows 1 and 3, and columns 2 to 4?
Use arr[[1, 3], 2:5] to select rows 1 and 3 with columns from 2 up to 4 (5 is exclusive).
Click to reveal answer
intermediate
Why is the order of fancy and slice indexing important in numpy?
The order matters because numpy first applies fancy indexing to select specific rows or columns, then applies slicing on the result. Changing order can change the output shape and values.
Click to reveal answer
What does arr[[0, 2], 1:3] select from a 2D numpy array arr?
ARows 1 and 3, columns 0 and 2
BColumns 0 and 2, rows 1 and 3
CRows 0 and 2, columns 1 and 2
DRows 0 to 2, columns 1 to 3
Which indexing method allows selecting elements at arbitrary positions in numpy?
AFancy indexing
BNone of the above
CBoolean indexing
DSlice indexing
What is the result shape of arr[[1, 2], 0:2] if arr has shape (4, 5)?
A(4, 2)
B(2, 2)
C(2, 5)
D(4, 5)
Can you combine fancy indexing on rows with slice indexing on columns in numpy?
AYes, like arr[[0, 3], 1:4]
BNo, numpy does not allow mixing
COnly if array is 1D
DOnly if array is 3D
What happens if you reverse the order: arr[1:3, [0, 2]] instead of arr[[1, 3], 0:2]?
ASelects all rows and columns
BSelects rows 1 and 3, columns 0 and 1
CRaises an error
DSelects rows 1 and 2, columns 0 and 2
Explain how to combine fancy and slice indexing to select specific rows and a range of columns from a 2D numpy array.
Think about selecting rows first, then columns.
You got /4 concepts.
    Describe the difference between fancy indexing and slice indexing and how they can be used together in numpy.
    Compare how each indexing method selects elements.
    You got /4 concepts.