Loading Python for AI Engineering...
A NumPy array is a metadata wrapper (shape, strides, dtype) over an underlying memory buffer. Slices return zero-copy views by altering strides; calls like reshape() or transpose() do not copy data unless non-contiguous strides force an allocation.
“Strides are like changing the reading cadence of a sentence: reading every 2nd word gives a new view without re-printing the book.”
Assuming array slicing creates a copy, then modifying the slice and inadvertently corrupting the training dataset in the source array.
import numpy as np
base = np.arange(12, dtype=np.int32).reshape(3, 4)
print("Shape:", base.shape) # (3, 4)
print("Strides:", base.strides) # (16, 4) -> 16 bytes/row, 4 bytes/col
view = base[:, ::2] # Take every 2nd column
print("View shares memory:", view.base is base) # True! Zero memory copied.
print("View strides:", view.strides) # (16, 8)