Why advanced indexing matters in NumPy - Performance Analysis
We want to see how using advanced indexing in numpy affects the time it takes to get data.
How does the time grow when we pick many items from an array in different ways?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000000)
indices = np.random.randint(0, 1000000, size=10000)
result = arr[indices]
This code picks 10,000 items from a large array using advanced indexing with a list of random positions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing elements at each index in the indices array.
- How many times: Once for each index in the indices list (10,000 times here).
As the number of indices grows, the time to get elements grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 element accesses |
| 100 | About 100 element accesses |
| 1000 | About 1000 element accesses |
Pattern observation: The time grows directly with how many items you pick.
Time Complexity: O(n)
This means the time to get elements grows in a straight line with the number of indices you use.
[X] Wrong: "Advanced indexing is instant no matter how many items I pick."
[OK] Correct: Each index requires accessing the array, so more indices mean more work and more time.
Understanding how indexing time grows helps you write faster code and explain your choices clearly in real projects.
"What if we used slicing instead of advanced indexing? How would the time complexity change?"
Practice
Solution
Step 1: Understand what advanced indexing does
Advanced indexing lets you pick many specific elements from an array using lists or arrays of indices.Step 2: Compare options with this understanding
Only It allows selecting multiple specific elements easily and efficiently. describes this benefit correctly; others describe unrelated features.Final Answer:
It allows selecting multiple specific elements easily and efficiently. -> Option AQuick Check:
Advanced indexing = easy multi-element selection [OK]
- Thinking it sorts or changes data types
- Confusing with basic slicing
- Assuming it converts arrays to lists
arr?Solution
Step 1: Recall advanced indexing syntax
Advanced indexing uses square brackets with a list or array of indices inside, like arr[[0, 2, 4]].Step 2: Check each option
arr[[0, 2, 4]] uses correct syntax. Options A, C, and D use invalid syntax for indexing.Final Answer:
arr[[0, 2, 4]] -> Option BQuick Check:
Use double brackets with list for advanced indexing [OK]
- Using parentheses instead of brackets
- Using slicing syntax incorrectly
- Using curly braces which are invalid
import numpy as np arr = np.array([10, 20, 30, 40, 50]) indices = [1, 3] print(arr[indices])
Solution
Step 1: Understand the array and indices
The array is [10, 20, 30, 40, 50]. The indices list is [1, 3].Step 2: Apply advanced indexing
arr[indices] selects elements at positions 1 and 3, which are 20 and 40.Final Answer:
[20 40] -> Option CQuick Check:
arr[[1,3]] = [20 40] [OK]
- Confusing indices with values
- Expecting a slice output
- Printing indices instead of array elements
import numpy as np arr = np.array([1, 2, 3, 4, 5]) indices = [0, 2, 5] print(arr[indices])
Solution
Step 1: Check the array size and indices
The array has 5 elements with indices 0 to 4. The indices list includes 5, which is invalid.Step 2: Understand the error cause
Accessing arr[5] causes an IndexError because index 5 is out of bounds.Final Answer:
Index 5 is out of bounds for the array. -> Option AQuick Check:
Index must be within array size [OK]
- Thinking list type causes error
- Assuming advanced indexing needs 2D arrays
- Ignoring out-of-range indices
arr = np.array([[1,2,3],[4,5,6],[7,8,9]]), how can you use advanced indexing to select elements 2, 6, and 7 from the array?Solution
Step 1: Identify positions of elements 2, 6, and 7
Element 2 is at (0,1), 6 at (1,2), and 7 at (2,0).Step 2: Use advanced indexing with row and column arrays
Using arr[[0,1,2], [1,2,0]] selects these elements by pairing rows and columns.Final Answer:
arr[[0,1,2], [1,2,0]] -> Option DQuick Check:
Pair row and column indices to select elements [OK]
- Using invalid indices out of range
- Trying to slice instead of advanced indexing
- Misaligning row and column index arrays
Ready to prove it?
Pass the test and this pattern moves unknown
