ML Foundations
Use NumPy empty only when you overwrite every element
numpy.empty returns uninitialized values. Allocate with it only when your code writes every slot before any read; otherwise choose an initialized constructor such as zeros.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
An empty array is allocated, not initialized
The name `numpy.empty` can sound as if it creates an empty collection. It instead allocates an ndarray with the requested shape and dtype without initializing ordinary entries. Those values are arbitrary, so they are not guaranteed to be zero, repeatable, or meaningful. Printing an `empty` array and interpreting the displayed numbers is therefore not a valid way to obtain a default value. [1]
There is one documented dtype-specific exception: object arrays are initialized to `None`. For numeric buffers, the safe rule is simple: do not read any element until your program has written a defined value to it. This is a correctness rule, not a performance measurement. [1]
References: [1]
Use empty when a complete write immediately follows
A preallocated buffer is useful when the next operation deterministically fills all of its positions. The example computes a same-shaped output and assigns the complete result into `buffer[:]`. Only after that full-slice assignment does it inspect the values. The assertion checks the intended transformation from the known input, so the result does not depend on whatever bits happened to occupy the new allocation. [1]
Partial writes need a stronger invariant: every location that can later be read must have been assigned on every control-flow path. If some elements are conditionally skipped, a buffer created by `empty` can carry arbitrary data into a later computation. This guide avoids probing those arbitrary values; it tests the safe full-write pattern and makes its initialization boundary visible.
Executed with Python 3.12.14 and NumPy 2.3.5 on CPU; assertions passed.
"""Allocate a NumPy buffer only when every element is written before reading."""
import numpy as np
values = np.arange(1, 7, dtype=np.int64)
buffer = np.empty(values.shape, dtype=values.dtype)
buffer[:] = values * 2
safe_accumulator = np.zeros(3, dtype=np.int64)
print("fully assigned buffer:", buffer.tolist())
print("zero initialized accumulator:", safe_accumulator.tolist())
assert buffer.tolist() == [2, 4, 6, 8, 10, 12]
assert safe_accumulator.tolist() == [0, 0, 0]
Recorded output
fully assigned buffer: [2, 4, 6, 8, 10, 12] zero initialized accumulator: [0, 0, 0]
References: [1]
Choose zeros when untouched entries need a defined value
If a buffer accumulates increments, starts as a mask, or may be read before every slot is assigned, `numpy.zeros` is the clearer constructor. It returns the requested shape filled with zero, giving skipped positions a defined starting value. The example contrasts that accumulator with the fully overwritten `empty` buffer. [2]
This does not establish that `empty` is faster for a particular application. NumPy notes that it may be marginally faster because it skips initialization, but this guide contains no timing test. Choose based on the write-before-read invariant first; benchmark a representative workload only if performance matters. The distinction is between arbitrary uninitialized entries and explicit zero initialization, not between two different meanings of an empty-shaped array. [1, 2]