Trait ndarray_stats::HistogramExt
source · pub trait HistogramExt<A, S>where
S: Data<Elem = A>,{
fn histogram(&self, grid: Grid<A>) -> Histogram<A>
where
A: Ord;
}
Expand description
Extension trait for ArrayBase
providing methods to compute histograms.
Required Methods
sourcefn histogram(&self, grid: Grid<A>) -> Histogram<A>where
A: Ord,
fn histogram(&self, grid: Grid<A>) -> Histogram<A>where
A: Ord,
Returns the histogram
for a 2-dimensional array of points M
.
Let (n, d)
be the shape of M
:
n
is the number of points;d
is the number of dimensions of the space those points belong to. It follows that every column inM
is ad
-dimensional point.
For example: a (3, 4) matrix M
is a collection of 3 points in a
4-dimensional space.
Important: points outside the grid are ignored!
Panics if d
is different from grid.ndim()
.
Example:
extern crate ndarray_stats;
#[macro_use(array)]
extern crate ndarray;
extern crate noisy_float;
use ndarray_stats::{
HistogramExt,
histogram::{
Histogram, Grid, GridBuilder,
Edges, Bins,
strategies::Sqrt},
};
use noisy_float::types::{N64, n64};
let observations = array![
[n64(1.), n64(0.5)],
[n64(-0.5), n64(1.)],
[n64(-1.), n64(-0.5)],
[n64(0.5), n64(-1.)]
];
let grid = GridBuilder::<Sqrt<N64>>::from_array(&observations).build();
let expected_grid = Grid::from(
vec![
Bins::new(Edges::from(vec![n64(-1.), n64(0.), n64(1.), n64(2.)])),
Bins::new(Edges::from(vec![n64(-1.), n64(0.), n64(1.), n64(2.)])),
]
);
assert_eq!(grid, expected_grid);
let histogram = observations.histogram(grid);
let histogram_matrix = histogram.counts();
// Bins are left inclusive, right exclusive!
let expected = array![
[1, 0, 1],
[1, 0, 0],
[0, 1, 0],
];
assert_eq!(histogram_matrix, expected.into_dyn());