0
0
NumPydata~10 mins

Shuffling arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shuffling arrays
Start with original array
Call shuffle function
Randomly reorder elements
Return shuffled array
Use shuffled array for analysis
We start with an original array, then call a shuffle function that randomly reorders its elements, returning the shuffled array for further use.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)
This code shuffles the elements of the array in place and prints the new order.
Execution Table
StepActionArray State BeforeArray State AfterNotes
1Initialize array[1, 2, 3, 4, 5][1, 2, 3, 4, 5]Original array created
2Call np.random.shuffle(arr)[1, 2, 3, 4, 5][3, 1, 5, 2, 4]Elements reordered randomly (example)
3Print array[3, 1, 5, 2, 4][3, 1, 5, 2, 4]Shuffled array output
4End[3, 1, 5, 2, 4][3, 1, 5, 2, 4]Shuffling complete
💡 Shuffling stops after array is reordered in place.
Variable Tracker
VariableStartAfter shuffleFinal
arr[1, 2, 3, 4, 5][3, 1, 5, 2, 4][3, 1, 5, 2, 4]
Key Moments - 3 Insights
Why does the original array change after shuffle?
Because np.random.shuffle modifies the array in place, it does not create a new array. See execution_table step 2 where the array state changes directly.
Is the shuffle result always the same?
No, the shuffle is random each time you run it, so the order changes. The example in execution_table step 2 is just one possible outcome.
Can shuffle be used on multi-dimensional arrays?
Yes, but it only shuffles along the first axis (rows). The internal order of elements in each row stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what is the array state after shuffle?
A[1, 2, 3, 4, 5]
B[3, 1, 5, 2, 4]
C[5, 4, 3, 2, 1]
D[2, 3, 1, 5, 4]
💡 Hint
Check the 'Array State After' column in step 2 of execution_table.
At which step does the array get shuffled in place?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where np.random.shuffle is called in execution_table.
If we want to keep the original array unchanged, what should we do before shuffling?
ACreate a copy of the array and shuffle the copy
BShuffle directly with np.random.shuffle
CUse print before shuffle
DUse np.random.shuffle with a seed
💡 Hint
Recall that shuffle modifies the array in place as shown in variable_tracker.
Concept Snapshot
Shuffling arrays with numpy:
- Use np.random.shuffle(array) to reorder elements randomly.
- This changes the original array in place.
- For multi-dimensional arrays, shuffle affects only the first axis.
- To keep original data, shuffle a copy instead.
Full Transcript
We start with an original numpy array. Calling np.random.shuffle on it rearranges its elements randomly in place, changing the original array. The shuffled array can then be used for analysis or modeling. The shuffle is random each time, so results differ. For multi-dimensional arrays, only rows are shuffled, not elements inside rows. To preserve the original array, make a copy before shuffling.