ML Foundations
Choose row or element masks when filtering NumPy arrays
Preserve complete rows when a Boolean condition was computed for individual elements.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
A full-shape mask selects individual entries
For a two-dimensional feature matrix, a Boolean test such as x > 0 produces one truth value per element. Indexing with that full-shape mask returns the matching elements as a one-dimensional selection. If the next stage expects complete observations, that result has lost the original row boundaries. [1]
A row mask is a different contract: one Boolean per observation decides whether the entire row is retained. Decide whether the condition means every feature must pass or at least one must pass before reducing the element mask. [1, 2]
Reduce the feature axis before indexing
Run python examples/numpy_row_vs_element_mask.py. The two-by-three fixture contains one negative element in its first row. Indexing with the full mask yields five positive numbers. Reducing the mask with np.all along axis 1 produces two Boolean values, one for each row; indexing with that row mask keeps only the second row. [1, 2]
The assertions verify both values and shapes. The row selection retains shape (1, 3), which keeps the feature dimension intact. The example uses all because its rule requires every feature to be positive. An any reduction would express a different rule and retain rows having at least one positive feature.
If separate labels or IDs correspond to rows, apply the same row mask to those arrays after checking their row alignment. Filtering features and labels with unrelated masks can silently change the training pairs.
Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.
import numpy as np
x = np.array([[1, -1, 2], [3, 4, 5]])
mask = x > 0
elements = x[mask]
row_mask = np.all(mask, axis=1)
rows = x[row_mask]
assert elements.tolist() == [1, 2, 3, 4, 5]
assert rows.tolist() == [[3, 4, 5]]
assert elements.shape == (5,)
assert rows.shape == (1, 3)
print("element mask shape:", mask.shape)
print("selected elements:", elements.tolist())
print("row mask:", row_mask.tolist())
print("selected rows:", rows.tolist())
Recorded output
element mask shape: (2, 3) selected elements: [1, 2, 3, 4, 5] row mask: [False, True] selected rows: [[3, 4, 5]]
Write down the axis meaning
This fixture assumes rows are observations and columns are features. In a tensor with batch, time and feature axes, identify which axes should be reduced; reducing every axis would produce one decision for the entire dataset rather than one per observation. [2]
The example uses finite integers and a small matrix. It does not define a missing-value policy, validate labels, or claim a performance improvement.