Table of Contents

Class WeightedSamplerBase<T>

Namespace
AiDotNet.Data.Sampling
Assembly
AiDotNet.dll

Base class for weighted samplers providing common weight-based functionality.

public abstract class WeightedSamplerBase<T> : DataSamplerBase, IWeightedSampler<T>, IDataSampler

Type Parameters

T

The numeric type for weights.

Inheritance
WeightedSamplerBase<T>
Implements
Derived
Inherited Members

Remarks

WeightedSamplerBase provides common functionality for samplers that use per-sample weights, including cumulative probability computation and weighted random selection.

Constructors

WeightedSamplerBase(IEnumerable<T>, int?, bool, int?)

Initializes a new instance of the WeightedSamplerBase class.

protected WeightedSamplerBase(IEnumerable<T> weights, int? numSamples = null, bool replacement = true, int? seed = null)

Parameters

weights IEnumerable<T>

The weight for each sample.

numSamples int?

Number of samples to draw per epoch.

replacement bool

Whether to sample with replacement.

seed int?

Optional random seed for reproducibility.

Fields

CumulativeProbabilities

Cumulative probabilities for weighted sampling.

protected double[] CumulativeProbabilities

Field Value

double[]

NumOps

Numeric operations for type T.

protected static readonly INumericOperations<T> NumOps

Field Value

INumericOperations<T>

NumSamplesOverride

Number of samples to draw per epoch.

protected int? NumSamplesOverride

Field Value

int?

ReplacementEnabled

Whether to sample with replacement.

protected bool ReplacementEnabled

Field Value

bool

WeightsArray

The weights for each sample.

protected T[] WeightsArray

Field Value

T[]

Properties

Length

Gets the total number of samples this sampler will produce per epoch.

public override int Length { get; }

Property Value

int

Remarks

This may differ from the dataset size for oversampling or undersampling strategies.

NumSamples

Gets or sets the number of samples to draw per epoch.

public int? NumSamples { get; set; }

Property Value

int?

Remarks

If null, defaults to the dataset size. Can be larger than dataset size when Replacement is true (oversampling).

Replacement

Gets or sets whether to sample with replacement.

public bool Replacement { get; set; }

Property Value

bool

Remarks

With replacement: samples can be selected multiple times per epoch. Without replacement: each sample appears at most once per epoch.

Weights

Gets or sets the weights for each sample.

public IReadOnlyList<T> Weights { get; set; }

Property Value

IReadOnlyList<T>

Remarks

Higher weights increase the probability of a sample being selected. Weights should be non-negative.

Methods

ComputeCumulativeProbabilities()

Computes cumulative probability distribution from weights. Override this method to customize probability computation in derived classes.

protected virtual void ComputeCumulativeProbabilities()

SampleWeightedIndex()

Samples an index based on the cumulative probabilities using binary search.

protected int SampleWeightedIndex()

Returns

int

A randomly selected index based on weights.