ML Foundations

Understand shared data in NumPy sliding windows

Check how overlapping window views respond when their source series changes.

Example results for Understand shared data in NumPy sliding windows. view shape: 2, 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.

A window table can share the series

A rolling-feature pipeline can form overlapping windows without making an independent copy of each window. NumPy sliding_window_view creates a view; overlapping positions can refer to the same source elements. Changing the source after preparing the windows can therefore change values read from multiple windows. [1]

The function defaults to a non-writeable view because overlapping locations can refer to the same memory. That prevents direct writes through the default view; it does not freeze the original array against changes. [1]

References: [1], [2]

Compare the view with a snapshot

Run python examples/numpy_sliding_window_source.py. A four-element series and a width-three window yield two overlapping rows. The example checks memory sharing, makes a copy, then changes the second source element. Both windows containing that source position reflect the edit; the copied snapshot retains its prior values. [1, 2]

Assertions check the observed values and the ownership relationship in this small fixture. Use a copy when a downstream stage needs an independent snapshot, and keep the original view when sharing is intended and the source lifetime is controlled. This is a data-ownership choice, not evidence of a speed advantage.

Window shape and overlap depend on the chosen axis and width. Here the input is one-dimensional, so the first dimension indexes windows and the second indexes positions within a window. [1]

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

import numpy as np
from numpy.lib.stride_tricks import sliding_window_view

series = np.array([1, 2, 3, 4])
windows = sliding_window_view(series, 3)
snapshot = windows.copy()
assert np.shares_memory(series, windows)
assert not np.shares_memory(series, snapshot)
series[1] = 20
assert windows.tolist() == [[1, 20, 3], [20, 3, 4]]
assert snapshot.tolist() == [[1, 2, 3], [2, 3, 4]]
print("view shape:", windows.shape)
print("shares source:", np.shares_memory(series, windows))
print("windows after source edit:", windows.tolist())
print("snapshot after source edit:", snapshot.tolist())

Recorded output

view shape: (2, 3)
shares source: True
windows after source edit: [[1, 20, 3], [20, 3, 4]]
snapshot after source edit: [[1, 2, 3], [2, 3, 4]]

References: [1], [2]

Set boundaries before model training

The example concerns memory ownership, not a train-test split. Overlapping windows can also share observations across a split; decide how time boundaries and targets should be assigned before evaluating a forecasting pipeline.

This fixture uses a short finite integer series and does not test memory peaks, mutable views enabled explicitly, multidimensional axes or model accuracy. Exact memory-overlap checks can be expensive for complicated arrays; use them thoughtfully outside small examples. [2]

References: [1], [2]

Sources

  1. NumPy sliding_window_view
  2. NumPy shares_memory