ML Foundations
Repeat rows or repeat the whole NumPy batch?
Keep features and labels aligned when repeating rows or tiling a batch.
![Example results for Repeat rows or repeat the whole NumPy batch?. repeat rows: [10, 11], [10, 11], [20, 21], [20, 21]](/assets/covers/numpy-repeat-tile-row-alignment.png)
Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
Matching shapes can hide different row orders
A feature table may need to be expanded while keeping each row aligned with its label. Repeating each row twice produces the order A, A, B, B. Repeating the entire two-row batch twice produces A, B, A, B. Both results have four rows, so a shape check alone cannot identify the intended order. [1, 2]
NumPy repeat repeats elements or slices along a chosen axis. With a two-dimensional feature table, axis=0 repeats rows. Omitting the axis flattens the input before repetition, changing its structure. [1]
Repeat features and labels together
Run python examples/numpy_repeat_tile_row_alignment.py. The example repeats the feature rows and labels separately with the same repeat count. It also tiles the complete feature batch and the complete label vector. The printed output shows the resulting order for each method. [1, 2]
The two feature results have the same shape but different row order. Their label arrays must follow the same transformation as their associated features. The assertions check the fixed fixture and its expected label order; the printed rows make the alignment visible.
Executed with Python 3.12.14; stdout and checksum are recorded in private evidence.
import numpy as np
features = np.array([[10, 11], [20, 21]])
labels = np.array([1, 2])
repeated_features = np.repeat(features, 2, axis=0)
repeated_labels = np.repeat(labels, 2)
tiled_features = np.tile(features, (2, 1))
tiled_labels = np.tile(labels, 2)
print("repeat rows:", repeated_features.tolist())
print("repeat labels:", repeated_labels.tolist())
print("tile rows:", tiled_features.tolist())
print("tile labels:", tiled_labels.tolist())
assert repeated_features.shape == tiled_features.shape == (4, 2)
assert repeated_labels.tolist() == [1, 1, 2, 2]
assert tiled_labels.tolist() == [1, 2, 1, 2]
Recorded output
repeat rows: [[10, 11], [10, 11], [20, 21], [20, 21]] repeat labels: [1, 1, 2, 2] tile rows: [[10, 11], [20, 21], [10, 11], [20, 21]] tile labels: [1, 2, 1, 2]
Choose axes for the actual operation
For the two-dimensional table in this example, tile with reps=(2, 1) repeats the full row block twice while preserving its columns. Higher-dimensional inputs need a deliberate repetition tuple: tile may promote the input or repetition tuple to reconcile dimensions. [2]
This fixture uses small integer arrays and materializes expanded arrays. If the downstream calculation only needs compatible shapes, investigate broadcasting before making copies. NumPy recommends broadcasting operations where appropriate. This example makes no performance claim. [2]
References: [2]