ML Foundations

Set NumPy digitize right to control bin-edge ownership

For increasing bins, numpy.digitize(right=False) includes the left edge and excludes the right. Toggle right when your interval convention requires the opposite.

Example results for Set NumPy digitize right to control bin-edge ownership. right=False bins: 0, 1, 2, 3, 4, 4
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.

Write down which side owns an edge

Binning usually looks straightforward until a value lands exactly on an edge. For monotonically increasing `bins`, `numpy.digitize(..., right=False)` uses intervals that include the left boundary and exclude the right: `bins[i-1] <= x < bins[i]`. Setting `right=True` changes the comparison to `bins[i-1] < x <= bins[i]`. A value equal to an edge therefore moves to a neighboring bin when the setting changes. [1]

Values outside the edge range receive index 0 or `len(bins)` as appropriate. Those are sentinel positions relative to the supplied edges, not extra documented interval definitions in this example. Decide whether endpoints should be clipped, rejected or retained before using returned indices to select a histogram or lookup table. [1]

References: [1]

Test exact edges as well as interior values

The example deliberately includes -1, every edge from 0 through 3, and 4. With `right=False`, the exact edge 1 is assigned to index 2; with `right=True` it is assigned to index 1. The final edge 3 moves from index 4 to index 3. Those outputs make interval ownership visible instead of testing only values safely inside a bin. [1]

For increasing bins, the default result also matches `numpy.searchsorted(bins, values, side="right")`. NumPy documents `digitize` in terms of `searchsorted`, with the side choice depending on `right`. The script asserts this relationship for the sample while separately checking the opposite edge policy. [1, 2]

Executed with Python 3.12.14 and NumPy 2.3.5 on CPU; assertions passed.

"""Make NumPy digitize interval-edge ownership explicit."""
import numpy as np

edges = np.array([0, 1, 2, 3])
values = np.array([-1, 0, 1, 2, 3, 4])
left_closed = np.digitize(values, edges, right=False)
right_closed = np.digitize(values, edges, right=True)
searchsorted = np.searchsorted(edges, values, side="right")

print("right=False bins:", left_closed.tolist())
print("right=True bins:", right_closed.tolist())
print("searchsorted right:", searchsorted.tolist())

assert left_closed.tolist() == [0, 1, 2, 3, 4, 4]
assert right_closed.tolist() == [0, 0, 1, 2, 3, 4]
assert left_closed.tolist() == searchsorted.tolist()

Recorded output

right=False bins: [0, 1, 2, 3, 4, 4]
right=True bins: [0, 0, 1, 2, 3, 4]
searchsorted right: [0, 1, 2, 3, 4, 4]

References: [1], [2]

Keep sortedness and index use explicit

`digitize` requires a one-dimensional monotonic `bins` array and raises `ValueError` if the bins are not monotonic. Do not treat its output as a categorical label unless the downstream mapping accounts for the zero and length sentinels. The exact bin numbering matters when those indices feed a weight table, bucket count or interval label. [1]

This guide covers increasing integer edges and the two right settings. Decreasing bins follow a different documented comparison table; floating-point tolerances, NaNs and domain-specific clipping are not tested here. The reliable practice is to include exact boundaries in a small fixture and assert the resulting indices for the convention your application actually needs. [1, 2]

References: [1], [2]

Sources

  1. NumPy: digitize
  2. NumPy: searchsorted