Skip to main content

digitize

Function digitize 

Source
pub fn digitize<T>(
    array: ArrayView<'_, T, Ix1>,
    bins: ArrayView<'_, T, Ix1>,
    right: bool,
    result_type: &str,
) -> Result<Array<usize, Ix1>, &'static str>
where T: Clone + Float + FromPrimitive,
Expand description

Return the indices of the bins to which each value in input array belongs.

§Arguments

  • array - Input array
  • bins - Array of bin edges
  • right - Indicates whether the intervals include the right or left bin edge
  • result_type - Whether to return the indices (‘indices’) or the bin values (‘values’)

§Returns

Array of indices or values depending on result_type

§Examples

use ::ndarray::array;
use scirs2_core::ndarray_ext::stats::digitize;

let data = array![1.2, 3.5, 5.1, 0.8, 2.9];
let bins = array![1.0, 3.0, 5.0];
let indices = digitize(data.view(), bins.view(), false, "indices").expect("Operation failed");

assert_eq!(indices[0], 1); // 1.2 is in the first bin (1.0 <= x < 3.0)
assert_eq!(indices[1], 2); // 3.5 is in the second bin (3.0 <= x < 5.0)
assert_eq!(indices[2], 3); // 5.1 is after the last bin (>= 5.0)
assert_eq!(indices[3], 0); // 0.8 is before the first bin (< 1.0)
assert_eq!(indices[4], 1); // 2.9 is in the first bin (1.0 <= x < 3.0)

This function is equivalent to NumPy’s np.digitize function.