Function classify::get_jenks_classification
source · [−]pub fn get_jenks_classification<T: ToPrimitive>(
num_bins: usize,
data: &[T]
) -> Classification
Expand description
Returns a Classification object following the Jenks Natural Breaks algorithm given the desired number of bins and one-dimensional data
Arguments
num_bins
- An integer (usize) representing the desired number of binsdata
- 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
Examples
use classify::get_jenks_classification;
use classify::{Classification, Bin};
use rand::prelude::*;
use rand::rngs::StdRng;
let data: Vec<usize> = vec![1, 2, 4, 5, 7, 8];
let num_bins = 3;
let result: Classification = get_jenks_classification(num_bins, &data);
let expected: Classification = vec![
Bin{bin_start: 1.0, bin_end: 4.0, count: 2},
Bin{bin_start: 4.0, bin_end: 7.0, count: 2},
Bin{bin_start: 7.0, bin_end: 8.0, count: 2}
];
assert!(result == expected);