Shuffling arrays in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to shuffle an array changes as the array gets bigger.
Specifically, how does the work grow when we shuffle more items?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1_000_000)
np.random.shuffle(arr)
This code creates an array of one million numbers and then shuffles them randomly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Swapping elements in the array during the shuffle.
- How many times: Each element is visited once to swap with another element.
As the array size grows, the number of swaps grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 swaps |
| 100 | About 100 swaps |
| 1000 | About 1000 swaps |
Pattern observation: The work grows linearly with the number of elements.
Time Complexity: O(n)
This means the time to shuffle grows directly in proportion to the array size.
[X] Wrong: "Shuffling takes the same time no matter how big the array is."
[OK] Correct: Actually, each element must be swapped once, so bigger arrays take more time.
Knowing how shuffling scales helps you understand data preparation steps in real projects and shows you can analyze common array operations.
"What if we shuffled a 2D array instead of a 1D array? How would the time complexity change?"
Practice
np.random.shuffle() do to a NumPy array?Solution
Step 1: Understand the function purpose
np.random.shuffle()is designed to mix elements randomly within the array.Step 2: Check how it modifies the array
This function changes the original array directly (in-place), not creating a new one.Final Answer:
Randomly rearranges the elements of the array in-place -> Option AQuick Check:
Shuffle = random in-place rearrangement [OK]
- Thinking shuffle returns a new array
- Confusing shuffle with sorting
- Assuming shuffle reverses elements
arr?Solution
Step 1: Identify the correct function call
The shuffle function is inside thenp.randommodule, so it must be called asnp.random.shuffle().Step 2: Check the parameters
It takes the array as the only argument. The axis parameter is not valid for 1D arrays.Final Answer:
np.random.shuffle(arr) -> Option DQuick Check:
Correct shuffle syntax = np.random.shuffle(array) [OK]
- Using np.shuffle instead of np.random.shuffle
- Calling shuffle as a method on the array
- Passing axis parameter incorrectly
import numpy as np arr = np.array([1, 2, 3, 4, 5]) np.random.shuffle(arr) print(arr)
Solution
Step 1: Understand shuffle effect on array
np.random.shuffle(arr)rearranges elements randomly in-place, soarrchanges order.Step 2: Predict output of print
Since shuffle is random, the printed array will be a shuffled version of the original, not sorted or reversed.Final Answer:
A randomly shuffled version of [1 2 3 4 5] -> Option CQuick Check:
Shuffle output = random order of original array [OK]
- Expecting original order after shuffle
- Thinking shuffle returns a new array
- Assuming shuffle sorts or reverses
import numpy as np arr = np.array([[1, 2], [3, 4]]) np.random.shuffle(arr, axis=1) print(arr)
Solution
Step 1: Check shuffle function parameters
np.random.shuffle()only accepts the array argument; it does not have an axis parameter.Step 2: Understand shuffle behavior on 2D arrays
Shuffle works in-place on the first axis of multi-dimensional arrays by default, no axis argument needed.Final Answer:
shuffle does not accept an axis argument -> Option AQuick Check:
shuffle axis param invalid = error [OK]
- Passing axis argument to shuffle
- Thinking shuffle returns a new array
- Believing shuffle only works on 1D arrays
data = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90],
[15, 25, 35],
[45, 55, 65]])
You want to shuffle the samples (rows) but keep the feature order intact. Which code correctly does this?Solution
Step 1: Understand shuffle on 2D arrays
np.random.shuffle()shuffles along the first axis (rows) in-place, which is what we want.Step 2: Evaluate other options
np.random.shuffle(data.T) shuffles columns (wrong axis), np.random.permutation(data) returns a new shuffled array (not in-place), np.random.shuffle(data, axis=1) is invalid (axis param not accepted).Final Answer:
np.random.shuffle(data) -> Option BQuick Check:
Shuffle rows in-place = np.random.shuffle(data) [OK]
- Trying to shuffle columns by transposing
- Expecting shuffle to return a new array
- Passing axis parameter to shuffle
