NumPy digitize assigns interval indices while keeping boundary rules explicit
A sequence of boundaries can describe intervals without changing the original observations. NumPy digitize reports which interval index corresponds to each input value. Academia Master Trader presents this English lesson for readers in Yemen, using constructed numeric arrays to explain increasing boundaries, endpoint inclusion and the difference between interval labels and interval counts.
Increasing boundaries define adjacent intervals
After import numpy as np, define edges = np.array([0,10,20,30]) and x = np.array([4,10,17,30]). The call np.digitize(x,edges) returns [1,2,2,4].
With increasing boundaries and the default right=False, index i describes edges[i-1] <= value < edges[i] for an interior interval. Four belongs to interval one, between zero inclusive and ten exclusive. Ten and seventeen belong to interval two, between ten inclusive and twenty exclusive.
The output numbers are interval indices. They are not the input values, interval widths or counts of observations. Two observations receive index two here, but digitize does not replace that pair with a frequency count.

The right argument changes which endpoint is included
The call np.digitize(x,edges,right=True) returns [1,1,2,3]. For increasing boundaries, an interior interval now follows edges[i-1] < value <= edges[i]. Ten belongs to interval one rather than interval two.
Thirty also changes its result. Under the default rule, thirty is not inside the final interior interval because that interval excludes its right endpoint. With right set to True, thirty belongs to interval three, which includes thirty and excludes twenty.
Four and seventeen do not sit on the boundaries, so their interval indices remain unchanged. Compare exact endpoint values separately from values strictly between boundaries when explaining this argument.
Values beyond the boundary range need separate handling
The call np.digitize([-3,35],edges) returns [0,4]. Zero and the number of boundaries are possible results for values outside the covered interior intervals. They should not be mistaken for ordinary interior intervals with two available endpoints.
For example, using returned index four directly as edges[4] would exceed this array's valid element indices. Before retrieving lower and upper boundaries, separate results that fall at the outer index values. The rule should match the intended treatment of values beyond the supplied range.
Digitize does not change either the observations or the supplied boundaries. Keeping both arrays beside the labels makes a simple verification possible: inspect one observation, locate its neighbouring boundaries and apply the chosen inequality. Repeat that reasoning for boundary values separately.
Input shape is preserved and boundaries must be monotonic
The call np.digitize([[4,17],[10,25]],edges) returns [[1,2],[2,3]]. The output has the same shape as the input values, so each interval label remains aligned with its original observation.
Boundaries must form a one dimensional monotonic sequence. Decreasing boundaries are supported, but their interval inequalities differ from the increasing example. An unordered boundary sequence raises ValueError; digitize does not silently sort it. Keep the boundary direction visible when describing any result.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the boundary sequence and endpoint rule beside every set of interval indices.