Skip to main content

random_undersample

Function random_undersample 

Source
pub fn random_undersample(
    data: &Array2<f64>,
    targets: &Array1<f64>,
    random_seed: Option<u64>,
) -> Result<(Array2<f64>, Array1<f64>)>
Expand description

Performs random undersampling to balance class distribution

Randomly removes samples from majority classes to match the minority class size. This reduces the overall dataset size but maintains balance.

§Arguments

  • data - Feature matrix (n_samples, n_features)
  • targets - Target values for each sample
  • random_seed - Optional random seed for reproducible sampling

§Returns

A tuple containing the undersampled (data, targets) arrays

§Examples

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_datasets::utils::random_undersample;

let data = Array2::from_shape_vec((6, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]).expect("Operation failed");
let targets = Array1::from(vec![0.0, 0.0, 1.0, 1.0, 1.0, 1.0]); // Imbalanced: 2 vs 4
let (balanced_data, balanced_targets) = random_undersample(&data, &targets, Some(42)).expect("Operation failed");
// Now both classes have 2 samples each