ML Foundations

Choose the endpoint contract for a NumPy grid

Use an explicit point count and endpoint policy when constructing a floating-point grid.

Example results for Choose the endpoint contract for a NumPy grid. arange count and last: 5 0.8
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.

Step size and point count express different requirements

A numerical grid may need a specified step, or it may need a fixed number of values with a clear endpoint rule. NumPy arange takes a step and normally describes a half-open interval. NumPy linspace takes a point count and includes the endpoint by default. [1, 2]

For floating-point arange arguments, rounding can affect the output length and endpoint behavior. If the number of points and whether the final endpoint is included are the important requirements, specify them directly with linspace. [1, 2]

References: [1], [2]

Compare the contracts

Run python examples/numpy_grid_endpoint_contract.py. For this fixture, arange from zero to one with a step of 0.2 produces five values ending near 0.8. Linspace with six points and endpoint enabled includes one. With five points and endpoint disabled, it produces five values ending near 0.8. [1, 2]

The script prints rounded last values for readability and checks the count and endpoint on the two linspace results. This fixture demonstrates the two API contracts; it does not imply that every floating-point step produces an exactly represented decimal. [1, 2]

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

import numpy as np

by_step = np.arange(0.0, 1.0, 0.2)
with_endpoint = np.linspace(0.0, 1.0, num=6, endpoint=True)
without_endpoint = np.linspace(0.0, 1.0, num=5, endpoint=False)
print('arange count and last:', len(by_step), round(float(by_step[-1]), 6))
print('inclusive count and last:', len(with_endpoint), float(with_endpoint[-1]))
print('exclusive count and last:', len(without_endpoint), round(float(without_endpoint[-1]), 6))
assert len(with_endpoint) == 6 and with_endpoint[-1] == 1.0
assert len(without_endpoint) == 5 and without_endpoint[-1] < 1.0

Recorded output

arange count and last: 5 0.8
inclusive count and last: 6 1.0
exclusive count and last: 5 0.8

References: [1], [2]

Check downstream assumptions

The number of intervals is one fewer than the number of points when both endpoints are included. Pick the count based on the downstream sampling or plotting requirement, and check the resulting shape before pairing the grid with another array.

This example uses finite one-dimensional floating-point inputs and a positive interval. It does not cover integer dtype conversion, non-finite inputs or performance. Linspace controls the requested count and endpoint policy; it does not make every intermediate floating-point value exact. [2]

References: [2]

Sources

  1. NumPy arange
  2. NumPy linspace