ML Foundations

Keep the feature shape when no NumPy batches remain

Handle an empty list of batches while preserving the expected two-dimensional feature contract.

Example results for Keep the feature shape when no NumPy batches remain. empty shape: 0, 3
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.

No batches differs from an empty batch

A filtering stage can return no batches at all, or it can return a batch whose row dimension is zero. Those are different inputs to a join operation. NumPy concatenate needs a sequence of arrays, and it joins their existing axes; it cannot infer a feature width from an empty sequence. [1]

If downstream code expects a two-dimensional matrix, returning a bare empty list or a one-dimensional empty array changes the shape contract. The intended empty result for three features has shape (0, 3).

References: [1], [2]

Provide the known feature width

Run python examples/numpy_empty_batches.py. The helper returns an empty float64 array with the known feature width when the batch list is empty. Otherwise it concatenates along the row axis. A zero-row array with shape (0, 3) can participate in that join alongside nonempty arrays of the same width. [1, 2]

Assertions check the empty shape, combined shape and row values. NumPy empty normally allocates uninitialized values; the zero-row result contains no elements to read. The caller supplies the width and the example fixes the dtype instead of attempting to infer either from absent data. [2]

This helper assumes that every supplied batch has the expected width and a compatible dtype. In production, validate those assumptions at the input boundary and decide whether dtype conversion is allowed. Concatenate requires matching shapes on axes other than the concatenation axis. [1]

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

import numpy as np

def join_batches(batches, feature_count):
    if not batches:
        return np.empty((0, feature_count), dtype=np.float64)
    return np.concatenate(batches, axis=0)

empty = join_batches([], 3)
batches = [np.array([[1., 2., 3.]]), np.empty((0, 3)), np.array([[4., 5., 6.]])]
joined = join_batches(batches, 3)
assert empty.shape == (0, 3)
assert joined.shape == (2, 3)
assert joined.tolist() == [[1., 2., 3.], [4., 5., 6.]]
print("empty shape:", empty.shape)
print("joined shape:", joined.shape)
print("joined rows:", joined.tolist())

Recorded output

empty shape: (0, 3)
joined shape: (2, 3)
joined rows: [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]

References: [1], [2]

Keep labels and empty-result semantics explicit

If each batch has a separate label array, preserve the same row ordering and apply corresponding filtering to both. An empty feature matrix alone does not establish that labels and features are aligned.

The fixture covers two-dimensional float arrays and a known feature count. It does not cover sparse matrices, streaming storage, schema discovery or performance. Decide whether an empty result is acceptable for the calling application or should instead cause an explicit error.

References: [1], [2]

Sources

  1. NumPy concatenate
  2. NumPy empty