Chapter 3: Random Permutations

What is a permutation? (quick honest explanation)

A permutation is simply a rearrangement of the elements of a sequence.

Examples:

  • Original: [A, B, C, D]
  • One permutation: [B, D, A, C]
  • Another: [D, A, C, B]
  • etc.

There are n! possible permutations of n distinct items.

NumPy gives us two main ways to create random permutations:

Method What it does Modifies original? Returns
np.random.permutation() Returns a new randomly shuffled copy No new array
np.random.shuffle() Shuffles in place (modifies original) Yes None

1. np.random.permutation() — most commonly used

Creates a new shuffled copy — original stays unchanged.

Python

Important observation — every time you run it → different order (unless seeded)

Python

Very common pattern — permuting indices

Python

This is exactly what train_test_split does behind the scenes.

2. np.random.shuffle() — shuffles in place

Modifies the array directly — does not return anything.

Python

Very common mistake students make:

Python

Correct usage:

Python

3. Quick comparison table (very useful to remember)

Property np.random.permutation() np.random.shuffle()
Returns new shuffled array None
Modifies original? No Yes
Can take integer N? Yes — creates 0..N-1 shuffled No
Memory usage creates copy no extra memory
Most common use case creating shuffled indices, new copy shuffling existing dataset in place

4. Special & very useful feature of permutation()

You can pass an integer instead of an array!

Python

This is extremely common when you want to shuffle indices without creating the full arange first.

5. Realistic patterns you will use again and again

Pattern 1 – Shuffle dataset before training

Python

Pattern 2 – Create k-fold cross-validation indices

Python

Pattern 3 – Random sampling without replacement

Python

Pattern 4 – Shuffle rows of a matrix

Python

Summary – Quick Decision Guide

You want to… Best choice
Get a new shuffled version (keep original) np.random.permutation(arr)
Shuffle existing array in place (save memory) np.random.shuffle(arr)
Create random order of 0..n-1 np.random.permutation(n)
Shuffle rows of a 2D array either — shuffle() or permutation + indexing
Need shuffled indices for splitting np.random.permutation(len(data))

Final teacher advice

Always think about whether you need the original order preserved:

  • Need original later → use permutation()
  • Don’t need original + want to save memory → use shuffle()
  • Working with indices → permutation(n) is usually cleanest

Always set a seed when you want reproducible shuffling:

Python

Where would you like to go next?

  • Difference between permutation and choice (sampling)
  • Shuffling multi-dimensional arrays correctly
  • Random permutations in machine learning pipelines
  • Common bugs when shuffling labels/data separately
  • Mini-exercise: shuffle a dataset and create train/val splits

Just tell me what feels most useful or interesting right now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *