pub fn importance_sample(
weights: &Array1<f64>,
sample_size: usize,
replace: bool,
random_seed: Option<u64>,
) -> Result<Vec<usize>>
Expand description
Performs importance sampling based on provided weights
Samples indices according to the provided probability weights. Higher weights increase the probability of selection. This is useful for adaptive sampling where some samples are more important than others for training.
§Arguments
weights
- Probability weights for each sample (must be non-negative)sample_size
- Number of samples to drawreplace
- Whether to sample with replacementrandom_seed
- Optional random seed for reproducible sampling
§Returns
A vector of indices representing the importance-weighted sample
§Examples
use ndarray::Array1;
use scirs2_datasets::utils::importance_sample;
// Give higher weights to the last few samples
let weights = Array1::from(vec![0.1, 0.1, 0.1, 0.8, 0.9, 1.0]);
let indices = importance_sample(&weights, 3, false, Some(42)).unwrap();
assert_eq!(indices.len(), 3);
// Higher weighted samples should be more likely to be selected
let mut high_weight_count = 0;
for &idx in &indices {
if idx >= 3 { // Last three samples have higher weights
high_weight_count += 1;
}
}
// This should be true with high probability
assert!(high_weight_count >= 1);