Bird
Raised Fist0
NumPydata~10 mins

Shuffling arrays in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does np.random.shuffle() do to a NumPy array?
easy
A. Randomly rearranges the elements of the array in-place
B. Sorts the array elements in ascending order
C. Creates a new shuffled copy of the array without changing the original
D. Reverses the order of elements in the array

Solution

  1. Step 1: Understand the function purpose

    np.random.shuffle() is designed to mix elements randomly within the array.
  2. Step 2: Check how it modifies the array

    This function changes the original array directly (in-place), not creating a new one.
  3. Final Answer:

    Randomly rearranges the elements of the array in-place -> Option A
  4. Quick Check:

    Shuffle = random in-place rearrangement [OK]
Hint: Shuffle mixes elements inside the same array [OK]
Common Mistakes:
  • Thinking shuffle returns a new array
  • Confusing shuffle with sorting
  • Assuming shuffle reverses elements
2. Which of the following is the correct syntax to shuffle a 1D NumPy array named arr?
easy
A. np.random.shuffle(arr, axis=1)
B. np.shuffle(arr)
C. arr.shuffle()
D. np.random.shuffle(arr)

Solution

  1. Step 1: Identify the correct function call

    The shuffle function is inside the np.random module, so it must be called as np.random.shuffle().
  2. Step 2: Check the parameters

    It takes the array as the only argument. The axis parameter is not valid for 1D arrays.
  3. Final Answer:

    np.random.shuffle(arr) -> Option D
  4. Quick Check:

    Correct shuffle syntax = np.random.shuffle(array) [OK]
Hint: Use np.random.shuffle(array) to shuffle in-place [OK]
Common Mistakes:
  • Using np.shuffle instead of np.random.shuffle
  • Calling shuffle as a method on the array
  • Passing axis parameter incorrectly
3. What will be the output of the following code?
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)
medium
A. [1 2 3 4 5]
B. [5 4 3 2 1]
C. A randomly shuffled version of [1 2 3 4 5]
D. Error because shuffle returns a new array

Solution

  1. Step 1: Understand shuffle effect on array

    np.random.shuffle(arr) rearranges elements randomly in-place, so arr changes order.
  2. 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.
  3. Final Answer:

    A randomly shuffled version of [1 2 3 4 5] -> Option C
  4. Quick Check:

    Shuffle output = random order of original array [OK]
Hint: Shuffle changes order randomly, output varies each run [OK]
Common Mistakes:
  • Expecting original order after shuffle
  • Thinking shuffle returns a new array
  • Assuming shuffle sorts or reverses
4. Identify the error in the following code snippet:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
np.random.shuffle(arr, axis=1)
print(arr)
medium
A. shuffle does not accept an axis argument
B. arr must be 1D to shuffle
C. shuffle returns a new array, so assignment is needed
D. np.random.shuffle cannot shuffle 2D arrays

Solution

  1. Step 1: Check shuffle function parameters

    np.random.shuffle() only accepts the array argument; it does not have an axis parameter.
  2. 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.
  3. Final Answer:

    shuffle does not accept an axis argument -> Option A
  4. Quick Check:

    shuffle axis param invalid = error [OK]
Hint: np.random.shuffle has no axis parameter [OK]
Common Mistakes:
  • Passing axis argument to shuffle
  • Thinking shuffle returns a new array
  • Believing shuffle only works on 1D arrays
5. You have a 2D NumPy array representing 5 samples with 3 features each:
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?
hard
A. np.random.shuffle(data.T)
B. np.random.shuffle(data)
C. np.random.permutation(data)
D. np.random.shuffle(data, axis=1)

Solution

  1. Step 1: Understand shuffle on 2D arrays

    np.random.shuffle() shuffles along the first axis (rows) in-place, which is what we want.
  2. 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).
  3. Final Answer:

    np.random.shuffle(data) -> Option B
  4. Quick Check:

    Shuffle rows in-place = np.random.shuffle(data) [OK]
Hint: Shuffle rows by calling np.random.shuffle on 2D array [OK]
Common Mistakes:
  • Trying to shuffle columns by transposing
  • Expecting shuffle to return a new array
  • Passing axis parameter to shuffle