HOWTO · NumPy
How to NumPy Shuffle Two Arrays
There are three main methods that can be used to shuffle two corresponding arrays in Python, the sklearn.utils.shuffle() function, the numpy.random.shuffle() function, and the numpy.random.permutation() function.
On this page
Shuffling two related arrays means changing their row order together. If row i in one array describes row i in another—for example, a feature row and its label—both arrays must use the same permutation. Shuffling them independently breaks that relationship. The examples below use a two-column feature array and a label array; matching values in each row make the alignment visible.
Shuffle two arrays while keeping corresponding rows aligned
Apply one shared row order to both arrays. The first dimension must have the same length for each input. For arrays with multiple columns, the shared order selects whole rows, so the values within a row stay together.
Run this example with a fixed order so the matching feature row and label are easy to check:
import numpy as np
features = np.array([[10, 100], [20, 200], [30, 300]])
labels = np.array([1, 2, 3])
order = np.array([2, 0, 1])
print(features[order])
print(labels[order])
[[ 30 300]
[ 10 100]
[ 20 200]]
[3 1 2]
The fixed order is for illustration; use one order produced by a shuffle method below when you want a random arrangement.
Use sklearn.utils.shuffle()
sklearn.utils.shuffle() accepts multiple arrays and applies one consistent ordering, returning shuffled arrays for you to assign. With scikit-learn installed, first import the function with from sklearn.utils import shuffle, then call features_shuffled, labels_shuffled = shuffle(features, labels). Pass every related array in the same call; the resulting rows stay paired. This approach is convenient when scikit-learn is already part of the project. Its random order can vary between calls, and the helper returns results for assignment rather than rearranging the source arrays in place.
The import and call require scikit-learn, which is not available in every Python environment. When it is installed, run the import before the call and assign both returned arrays. This dependency-specific call is shown as syntax rather than as an executed example; the NumPy sections below provide runnable alternatives without scikit-learn.
Use one shared permutation with NumPy
numpy.random.permutation(features.shape[0]) creates a shuffled sequence of row indices. Store it in order, then select features[order] and labels[order]. NumPy advanced indexing produces indexed results, leaving the source arrays available in their original order. Keep order to reuse this exact arrangement, apply it to more arrays, or save it with the data for a later step. Generating and retaining the index order also avoids depending on a particular random-state interface.
Here one generated order indexes both arrays. The printed row order varies, while the feature-label pairs remain together:
import numpy as np
features = np.array([[10, 100], [20, 200], [30, 300]])
labels = np.array([1, 2, 3])
order = np.random.permutation(features.shape[0])
features_shuffled = features[order]
labels_shuffled = labels[order]
print(features_shuffled)
print(labels_shuffled)
Shuffle a shared index array with NumPy
numpy.random.shuffle() changes its input sequence in place. Create an index array with order = np.arange(features.shape[0]), shuffle that array with np.random.shuffle(order), and select both sources with features[order] and labels[order]. This keeps the source data intact while making the row order explicit. The approach works for additional arrays as well: index every aligned array with the same shuffled indices. Only order is shuffled in place; the indexed results are separate arrays. Keep a copy of order if other code may later modify it and you need to preserve this arrangement.
This example mutates only order; both source arrays are indexed with that same shuffled sequence:
import numpy as np
features = np.array([[10, 100], [20, 200], [30, 300]])
labels = np.array([1, 2, 3])
order = np.arange(features.shape[0])
np.random.shuffle(order)
features_shuffled = features[order]
labels_shuffled = labels[order]
print(features_shuffled)
print(labels_shuffled)
Check shapes, mutation, and reproducibility
The arrays must have compatible first dimensions: their lengths along axis 0 must match, though their remaining dimensions can differ. For example, a matrix of feature rows can be paired with a one-dimensional label array. Check the leading sizes before applying an index order so every index is valid for every array.
The three approaches differ in what they mutate. sklearn.utils.shuffle() returns shuffled arrays for assignment. numpy.random.permutation() returns a new index order, and using it for array indexing creates reordered results. numpy.random.shuffle() modifies the index array it receives; it does not directly shuffle the source arrays in these examples. In each case, correspondence is preserved only when all related arrays use the same order.
For reproducibility, retain the generated index array and apply it again to the original arrays, or save it alongside the data. Reusing the same index order recreates the same arrangement without relying on an unspecified random-state API. If you need a new independent shuffle, generate a new order. No particular printed ordering is promised by these examples.