ML Foundations

Align NumPy differences with their observations

Keep adjacent differences aligned with later observations and mark a prepended first value as artificial.

Example results for Align NumPy differences with their observations. later observations: 15, 11, 18
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.

Adjacent differences have one fewer value

For an ordered sequence of observations, the first difference is the second value minus the first. NumPy diff computes each following value minus its predecessor. A one-dimensional array with four observations therefore produces three first differences. Each difference naturally corresponds to the later observation in its pair. [1]

When pairing the differences with observation labels or timestamps, use the labels from the second observation onward. Pairing with the original labels without adjusting their length will misalign the data. Basic slicing can select that suffix. [2]

References: [1], [2]

Keep the first slot explicit

Run python examples/numpy_diff_alignment.py. The values 12, 15, 11 and 18 produce differences 3, minus 4 and 7, paired with the later values 15, 11 and 18. The assertions check the calculated values and the relationship between both representations. [1]

If a downstream array must retain four positions, prepend the first observation before taking differences. This creates a first output of zero because the first value is subtracted from itself. That zero is a placeholder, not an observed prior change. The accompanying validity mask marks it false. [1]

Executed with Python 3.12.14; stdout and checksum saved in private evidence.

import numpy as np

observations = np.array([12, 15, 11, 18], dtype=np.int64)
deltas = np.diff(observations)
print('later observations:', observations[1:].tolist())
print('actual differences:', deltas.tolist())
aligned = np.diff(observations, prepend=observations[0])
print('aligned with artificial first zero:', aligned.tolist())
print('valid difference mask:', [False] + [True] * len(deltas))
assert deltas.tolist() == [3, -4, 7]
assert aligned[1:].tolist() == deltas.tolist()

Recorded output

later observations: [15, 11, 18]
actual differences: [3, -4, 7]
aligned with artificial first zero: [0, 3, -4, 7]
valid difference mask: [False, True, True, True]

References: [1]

Choose boundary and dtype deliberately

This example assumes a nonempty ordered sequence and signed integers. Handle an empty input before reading the first element. If a real observation exists before the sequence, use that value only when it is actually the intended boundary. [1]

NumPy diff preserves unsigned integer arithmetic, so a decrease can wrap instead of becoming negative. Convert to an appropriate signed or floating type before differencing if that is the required interpretation. The appropriate type depends on the input range. [1]

References: [1]

Sources

  1. NumPy diff
  2. NumPy array slicing