pub fn get_quantile_classification<T: ToPrimitive>(
    num_bins: usize,
    data: &[T]
) -> Classification
Expand description

Returns a Classification object following the Quantile Breaks algorithm given the desired number of bins and one-dimensional data

Arguments

  • num_bins - An integer (usize) representing the desired number of bins
  • data - A reference to a collection of unsorted data points to generate a Classification for

Edge Cases

  • Inputting large u64/i64 data (near their max values) will result in loss of precision because data is being cast to f64
  • The maximum number of bins generated by this algorithm is the number of unique values in the dataset
  • If your dataset contains many duplicates, there is a chance that the number of bins produced by the algorithm differs from num_bins because duplicate breaks are removed

Examples

use classify::get_quantile_classification;
use classify::{Classification, Bin};

let data: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
let num_bins = 3;

let result: Classification = get_quantile_classification(num_bins, &data);
let expected: Classification = vec![
    Bin{bin_start: 1.0, bin_end: 3.5, count: 3},
    Bin{bin_start: 3.5, bin_end: 6.5, count: 3},
    Bin{bin_start: 6.5, bin_end: 9.0, count: 3}
];

assert!(result == expected);