odatse.util.resampling module
- class odatse.util.resampling.BinarySearch(weights: Iterable)[source]
Bases:
Resampler
A resampler that uses binary search to sample based on given weights.
Initialize the BinarySearch resampler with the given weights.
- Parameters:
weights (Iterable) – An iterable of weights.
- __init__(weights: Iterable)[source]
Initialize the BinarySearch resampler with the given weights.
- Parameters:
weights (Iterable) – An iterable of weights.
- _sample(r: float) int [source]
Perform a binary search to find the index corresponding to the given random number.
- Parameters:
r (float) – A random number scaled by the maximum weight.
- Returns:
The index corresponding to the random number.
- Return type:
int
- reset(weights: Iterable)[source]
Reset the resampler with new weights.
- Parameters:
weights (Iterable) – An iterable of weights.
- sample(rs: RandomState) int [source]
- sample(rs: RandomState, size) ndarray
Sample indices based on the weights.
- Parameters:
rs (np.random.RandomState) – A random state for generating random numbers.
size – The number of samples to generate. If None, a single sample is generated.
- Returns:
A single sampled index or an array of sampled indices.
- Return type:
int or np.ndarray
- class odatse.util.resampling.WalkerTable(weights: Iterable)[source]
Bases:
Resampler
A resampler that uses Walker’s alias method to sample based on given weights.
Initialize the WalkerTable resampler with the given weights.
- Parameters:
weights (Iterable) – An iterable of weights.
- __init__(weights: Iterable)[source]
Initialize the WalkerTable resampler with the given weights.
- Parameters:
weights (Iterable) – An iterable of weights.
- _sample(r: float) int [source]
Perform a sampling operation based on the given random number.
- Parameters:
r (float) – A random number scaled by the number of weights.
- Returns:
The index corresponding to the random number.
- Return type:
int
- reset(weights: Iterable)[source]
Reset the resampler with new weights.
- Parameters:
weights (Iterable) – An iterable of weights.
- sample(rs: RandomState) int [source]
- sample(rs: RandomState, size) ndarray
Sample indices based on the weights.
- Parameters:
rs (np.random.RandomState) – A random state for generating random numbers.
size – The number of samples to generate. If None, a single sample is generated.
- Returns:
A single sampled index or an array of sampled indices.
- Return type:
int or np.ndarray