Skip to main content

RandomRefExt

Trait RandomRefExt 

Source
pub trait RandomRefExt<A, D>
where D: Dimension,
{ // Required methods fn sample_axis( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, ) -> Array<A, D> where A: Copy, D: RemoveAxis; fn sample_axis_using<R>( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, rng: &mut R, ) -> Array<A, D> where R: Rng + ?Sized, A: Copy, D: RemoveAxis; }
Expand description

Extension trait for sampling from ArrayRef with random elements.

The default RNG is a fast, automatically seeded rng (currently rand::rngs::SmallRng, seeded from rand::rng).

Note that SmallRng is cheap to initialize and fast, but it may generate low-quality random numbers, and reproducibility is not guaranteed. See its documentation for information. You can select a different RNG with .sample_axis_using().

Required Methods§

Source

fn sample_axis( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, ) -> Array<A, D>
where A: Copy, D: RemoveAxis,

Sample n_samples lanes slicing along axis using the default RNG.

If strategy==SamplingStrategy::WithoutReplacement, each lane can only be sampled once. If strategy==SamplingStrategy::WithReplacement, each lane can be sampled multiple times.

Panics when:

  • creation of the RNG fails;
  • n_samples is greater than the length of axis (if sampling without replacement);
  • length of axis is 0.
use ndarray::{array, Axis};
use ndarray_rand::{RandomExt, SamplingStrategy};

let a = array![
    [1., 2., 3.],
    [4., 5., 6.],
    [7., 8., 9.],
    [10., 11., 12.],
];
// Sample 2 rows, without replacement
let sample_rows = a.sample_axis(Axis(0), 2, SamplingStrategy::WithoutReplacement);
println!("{:?}", sample_rows);
// Example Output: (1st and 3rd rows)
// [
//  [1., 2., 3.],
//  [7., 8., 9.]
// ]
// Sample 2 columns, with replacement
let sample_columns = a.sample_axis(Axis(1), 1, SamplingStrategy::WithReplacement);
println!("{:?}", sample_columns);
// Example Output: (2nd column, sampled twice)
// [
//  [2., 2.],
//  [5., 5.],
//  [8., 8.],
//  [11., 11.]
// ]
Source

fn sample_axis_using<R>( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, rng: &mut R, ) -> Array<A, D>
where R: Rng + ?Sized, A: Copy, D: RemoveAxis,

Sample n_samples lanes slicing along axis using the specified RNG rng.

If strategy==SamplingStrategy::WithoutReplacement, each lane can only be sampled once. If strategy==SamplingStrategy::WithReplacement, each lane can be sampled multiple times.

Panics when:

  • creation of the RNG fails;
  • n_samples is greater than the length of axis (if sampling without replacement);
  • length of axis is 0.
use ndarray::{array, Axis};
use ndarray_rand::{RandomExt, SamplingStrategy};
use ndarray_rand::rand::SeedableRng;
use rand_isaac::isaac64::Isaac64Rng;

// Get a seeded random number generator for reproducibility (Isaac64 algorithm)
let seed = 42;
let mut rng = Isaac64Rng::seed_from_u64(seed);

let a = array![
    [1., 2., 3.],
    [4., 5., 6.],
    [7., 8., 9.],
    [10., 11., 12.],
];
// Sample 2 rows, without replacement
let sample_rows = a.sample_axis_using(Axis(0), 2, SamplingStrategy::WithoutReplacement, &mut rng);
println!("{:?}", sample_rows);
// Example Output: (1st and 3rd rows)
// [
//  [1., 2., 3.],
//  [7., 8., 9.]
// ]

// Sample 2 columns, with replacement
let sample_columns = a.sample_axis_using(Axis(1), 1, SamplingStrategy::WithReplacement, &mut rng);
println!("{:?}", sample_columns);
// Example Output: (2nd column, sampled twice)
// [
//  [2., 2.],
//  [5., 5.],
//  [8., 8.],
//  [11., 11.]
// ]

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<A, D> RandomRefExt<A, D> for ArrayRef<A, D>
where D: Dimension,

Source§

fn sample_axis( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, ) -> Array<A, D>
where A: Copy, D: RemoveAxis,

Source§

fn sample_axis_using<R>( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, rng: &mut R, ) -> Array<A, D>
where R: Rng + ?Sized, A: Copy, D: RemoveAxis,

Implementors§