RandomUnderSampler<T>

Models & Types · AiDotNet.Augmentation.Tabular.Undersampling

Implements random undersampling to balance imbalanced datasets.

Implements: IUnderSampler<T>

For Beginners

Random undersampling reduces the majority class by randomly removing samples until the classes are balanced. This is the simplest undersampling method.

How It Works

How it works:

  • Count samples in majority and minority classes
  • Randomly select N samples from the majority class
  • N is determined by the target ratio

Advantages:

  • Simple and fast
  • Reduces training time by having fewer samples
  • May improve some classifiers by removing redundant samples

Disadvantages:

  • May discard useful information from the majority class
  • Can lead to underfitting if too many samples are removed
  • Random selection doesn’t consider sample importance

When to use:

  • When the majority class is very large and redundant
  • When training time is a concern
  • As a baseline before trying more sophisticated methods

Constructors

Constructor Summary
RandomUnderSampler(double samplingRatio, bool withReplacement, int? randomSeed) Creates a new random undersampler.

Properties

Property Summary
bool WithReplacement { get; } Gets whether to use replacement when sampling.
double SamplingRatio { get; } Gets the target ratio between minority and majority samples after undersampling.
int? RandomSeed { get; } Gets the random seed for reproducibility.

Methods

Method Summary
ValueTuple<Matrix<T>, Vector<T>> Undersample(Matrix<T> data, Vector<T> labels, T minorityLabel) Performs random undersampling on the majority class.