digitize#
- named_arrays.digitize(x, bins, axis=None, right=False)#
Thin wrapper around
numpy.digitize().Find the index of the bin along
axiswhich each element ofxfalls into.This function adds an
axisargument, which letsbinscarry axes besides the one it is monotonic along, wherenumpy.digitize()takes one-dimensionalbinsonly. Those other axes are batch axes, and each is binned independently of the rest.- Parameters:
x (float | Quantity | AbstractArray) – The values to bin.
bins (AbstractArray) – The edges of the bins, monotonic along
axis. They may increase or decrease, asnumpy.digitize()allows.axis (None | str) – The logical axis along which
binsis monotonic. IfNone,binsmust have exactly one axis.right (bool) – Whether each bin includes its right edge instead of its left.
- Returns:
The index of the bin each value falls into, carrying the axes of
xbroadcast against the batch axes of
bins.An index of zero means the value falls below every bin, and an index
of the number of edges means it falls above every bin.
- Return type:
See also
numpy.digitize()The function this one wraps.
searchsorted()The insertion point of each value.
histogram()The number of values in each bin.
Examples
Find which wavelength bin each of two spectral lines falls into.
import numpy as np import astropy.units as u import named_arrays as na edges = na.linspace(100, 200, axis="wavelength", num=6) * u.AA line = na.ScalarArray( ndarray=np.array([117, 185]) * u.AA, axes=("line",), ) na.digitize(line, edges, axis="wavelength")
ScalarArray( ndarray=[1, 5], shape={'line': 2}, )