np.choose() for conditional selection in NumPy - Time & Space Complexity
We want to understand how the time taken by np.choose() changes as the input size grows.
Specifically, how does the work increase when we select elements conditionally from multiple arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
choices = np.array([10, 20, 30])
index = np.array([0, 2, 1, 0, 2])
result = np.choose(index, [choices, choices*2, choices*3])
This code selects elements from three arrays based on the index array, creating a new array with chosen values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Selecting elements from multiple arrays using the
indexarray. - How many times: Once for each element in the
indexarray.
As the size of the index array grows, the number of selections grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 element selections |
| 100 | About 100 element selections |
| 1000 | About 1000 element selections |
Pattern observation: The work grows linearly with the number of elements to select.
Time Complexity: O(n)
This means the time taken grows directly in proportion to the number of elements you select.
[X] Wrong: "The time grows with the number of choice arrays, so more arrays means much slower."
[OK] Correct: The main cost depends on how many elements you select, not how many arrays you have. Adding more arrays adds a small fixed cost per element, but the growth is still mainly linear with the number of elements.
Understanding how array operations scale helps you write efficient data code and explain your choices clearly in interviews.
What if we replaced np.choose() with a loop that selects elements one by one? How would the time complexity change?