NumPy Random Permutation

NumPy offers two closely related tools, shuffle and permutation, for randomly reordering data, and knowing which one modifies your array and which one returns a new one prevents subtle bugs.

Reordering Data Randomly

Many tasks need data in a random order: shuffling rows before splitting a dataset into training and test sets, randomizing the order of quiz questions, or dealing a shuffled deck of cards. NumPy provides two functions for this - np.random.shuffle and np.random.permutation - and they are easy to mix up because both scramble a sequence, but they behave very differently.

Example

import numpy as np

np.random.seed(1)
original = np.array([10, 20, 30, 40, 50])

shuffled_copy = np.random.permutation(original)

print("Original:", original)
print("Shuffled copy:", shuffled_copy)
# 'original' is untouched - permutation returned a brand new array

np.random.shuffle: In-Place Reordering

np.random.shuffle rearranges the array it is given directly, in memory, and returns None. That means you never assign its result to a variable - you call it on the array and then read the same array afterward. For a multi-dimensional array, shuffle only reorders along the first axis, so a 2D array has its rows shuffled, not the values inside each row.

Example

import numpy as np

np.random.seed(2)
deck = np.array([1, 2, 3, 4, 5])

result = np.random.shuffle(deck)
print("Return value:", result)   # None
print("deck after shuffle:", deck)  # deck itself has changed

# Shuffling a 2D array reorders whole rows, not values inside a row
grid = np.array([[1, 2], [3, 4], [5, 6]])
np.random.shuffle(grid)
print(grid)

np.random.permutation: Returns a New Array

np.random.permutation leaves its input untouched and instead returns a new, shuffled array, which is safer when you still need the original order elsewhere in your program. It also accepts a single integer n instead of an array - in that case it returns a shuffled arrangement of range(n), which is a common way to generate a random ordering of indices without shuffling any real data yet.

Note: To split a dataset into train and test sets, generate shuffled indices with np.random.permutation(len(data)) and use them to index into your feature array and label array together, so both stay aligned to the same random order.
  • shuffle changes its argument in place; permutation returns a new array and leaves the input alone.
  • shuffle always returns None; permutation always returns the shuffled result.
  • shuffle only accepts an existing array or list; permutation also accepts a single integer n.
  • For a 2D array, both functions reorder rows (the first axis), never the values within a row.
Aspectshufflepermutation
Modifies input?Yes, in placeNo, returns a copy
Return valueNoneA new shuffled array
Accepts an integer n?NoYes - shuffles range(n)
Typical useShuffle data you no longer need in orderGenerate shuffled indices or a shuffled copy

Example

import numpy as np

np.random.seed(3)
features = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
labels = np.array([0, 1, 0, 1])

# Shuffle indices, not the arrays themselves, to keep them aligned
order = np.random.permutation(len(features))
print("Shuffled order:", order)

shuffled_features = features[order]
shuffled_labels = labels[order]
print(shuffled_features)
print(shuffled_labels)

Exercise: NumPy Random Permutation

What is the key difference between np.random.shuffle() and np.random.permutation()?