Trait ndarray_rand::RandomExt

source ·
pub trait RandomExt<S, A, D>
where S: RawData<Elem = A>, D: Dimension,
{ // Required methods fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D> where IdS: Distribution<S::Elem>, S: DataOwned<Elem = A>, Sh: ShapeBuilder<Dim = D>; fn random_using<Sh, IdS, R>( shape: Sh, distribution: IdS, rng: &mut R, ) -> ArrayBase<S, D> where IdS: Distribution<S::Elem>, R: Rng + ?Sized, S: DataOwned<Elem = A>, Sh: ShapeBuilder<Dim = D>; fn sample_axis( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, ) -> Array<A, D> where A: Copy, S: Data<Elem = A>, 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, S: Data<Elem = A>, D: RemoveAxis; }
Expand description

Constructors for n-dimensional arrays with random elements.

This trait extends ndarray’s ArrayBase and can not be implemented for other types.

The default RNG is a fast automatically seeded rng (currently rand::rngs::SmallRng, seeded from rand::thread_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 .random_using().

Required Methods§

source

fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>
where IdS: Distribution<S::Elem>, S: DataOwned<Elem = A>, Sh: ShapeBuilder<Dim = D>,

Create an array with shape dim with elements drawn from distribution using the default RNG.

Panics if creation of the RNG fails or if the number of elements overflows usize.

use ndarray::Array;
use ndarray_rand::RandomExt;
use ndarray_rand::rand_distr::Uniform;

let a = Array::random((2, 5), Uniform::new(0., 10.));
println!("{:8.4}", a);
// Example Output:
// [[  8.6900,   6.9824,   3.8922,   6.5861,   2.4890],
//  [  0.0914,   5.5186,   5.8135,   5.2361,   3.1879]]
source

fn random_using<Sh, IdS, R>( shape: Sh, distribution: IdS, rng: &mut R, ) -> ArrayBase<S, D>
where IdS: Distribution<S::Elem>, R: Rng + ?Sized, S: DataOwned<Elem = A>, Sh: ShapeBuilder<Dim = D>,

Create an array with shape dim with elements drawn from distribution, using a specific Rng rng.

Panics if the number of elements overflows usize.

use ndarray::Array;
use ndarray_rand::RandomExt;
use ndarray_rand::rand::SeedableRng;
use ndarray_rand::rand_distr::Uniform;
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);

// Generate a random array using `rng`
let a = Array::random_using((2, 5), Uniform::new(0., 10.), &mut rng);
println!("{:8.4}", a);
// Example Output:
// [[  8.6900,   6.9824,   3.8922,   6.5861,   2.4890],
//  [  0.0914,   5.5186,   5.8135,   5.2361,   3.1879]]
source

fn sample_axis( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, ) -> Array<A, D>
where A: Copy, S: Data<Elem = A>, 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, S: Data<Elem = A>, 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.]
// ]

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<S, A, D> RandomExt<S, A, D> for ArrayBase<S, D>
where S: RawData<Elem = A>, D: Dimension,

source§

fn random<Sh, IdS>(shape: Sh, dist: IdS) -> ArrayBase<S, D>
where IdS: Distribution<S::Elem>, S: DataOwned<Elem = A>, Sh: ShapeBuilder<Dim = D>,

source§

fn random_using<Sh, IdS, R>( shape: Sh, dist: IdS, rng: &mut R, ) -> ArrayBase<S, D>
where IdS: Distribution<S::Elem>, R: Rng + ?Sized, S: DataOwned<Elem = A>, Sh: ShapeBuilder<Dim = D>,

source§

fn sample_axis( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, ) -> Array<A, D>
where A: Copy, S: Data<Elem = A>, 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, S: Data<Elem = A>, D: RemoveAxis,

Implementors§