ML Foundations

Shuffle features and labels with one NumPy permutation

Preserve row alignment when shuffling paired feature and label arrays.

Three colored feature and label pairs travel along separate paths. The pairs arrive in a different order with each match intact.
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

Keep each row paired with its label

Features and labels often start in corresponding row order. If each array is shuffled separately, their rows can be paired with the wrong labels even though their lengths and shapes still look valid. Draw one permutation of row indices and apply it to every aligned array. [1, 2]

First check that all arrays have the same number of rows. This method preserves existing correspondence; it cannot repair features and labels that were already mismatched. [2]

References: [1], [2]

Apply one order to both arrays

Run python examples/numpy_shuffle_paired_arrays.py. The fixture gives each row a distinctive first feature equal to ten times its label. A seeded generator produces a permutation of the row positions, and that same order selects the feature rows and labels. The assertion checks the known relationship after selection. [1, 2]

Integer-array indexing creates selected arrays, so changing the selected data is not a way to update the original arrays. The seed makes this particular example reproducible in the tested environment; record the NumPy version and random generator when reproducing a run. [1, 2]

Executed with local Python and NumPy; stdout and checksum are recorded in private evidence.

import numpy as np

features = np.array([[10, 100], [20, 200], [30, 300], [40, 400]])
labels = np.array([1, 2, 3, 4])
assert len(features) == len(labels)
rng = np.random.default_rng(42)
order = rng.permutation(len(labels))
shuffled_features = features[order]
shuffled_labels = labels[order]
assert np.array_equal(shuffled_features[:, 0] // 10, shuffled_labels)
print("order:", order.tolist())
print("feature first column:", shuffled_features[:, 0].tolist())
print("labels:", shuffled_labels.tolist())
print("pairs aligned:", bool(np.array_equal(shuffled_features[:, 0] // 10, shuffled_labels)))

Recorded output

order: [3, 2, 1, 0]
feature first column: [40, 30, 20, 10]
labels: [4, 3, 2, 1]
pairs aligned: True

References: [1], [2]

Use the same order for related metadata

If sample weights, identifiers or other metadata correspond to the same rows, apply the same index order to them too. Check lengths before indexing, and retain identifiers if the pairing needs to be audited later. [2]

This example permutes a small in-memory array once. It does not implement a train-test split, stream a dataset, or establish that a particular shuffle is suitable for grouped or time-ordered data.

References: [2]

Sources

  1. NumPy Generator permutation
  2. NumPy indexing