sampling#

Functions

best_sample(df, k, feature_cols, *[, ...])

Repeats random sampling n_trials times and returns the most space-filling sample.

generate_weights(df, n, bias[, ...])

Generates a Pandas series of weights for each datum given a particular bias.

sample_with_bias(df, n[, replace, seed, ...])

Returns a random Pandas DataFrame sample of data points from a population with or without bias.

obsidian.experiment.sampling.best_sample(df, k, feature_cols, *, n_trials=500, bias=None, plot_weights=False, enforce=False, random_state=None, standardize=True, dropna=True, metric='hybrid')[source]#

Repeats random sampling n_trials times and returns the most space-filling sample.

df: DataFrame of candidates k: size of the design to pick feature_cols: columns that define “space” (numeric; one-hot encode cats if needed) bias: None | dict in the format {“column”: [lower, upper, weight]} passed to

generate_weights to bias sampling towards specified ranges.

obsidian.experiment.sampling.generate_weights(df, n, bias, plot_weights=False, enforce=False, replace=False)[source]#

Generates a Pandas series of weights for each datum given a particular bias.

df: DataFrame of candidates n: size of the design to pick bias: dictionary of biases in the format : {“column”: [lower_bound, upper_bound, relative_weight]}

  • Weight >1 increases sampling probability for in-range rows.

  • Weight <1 decreases it.

  • Weight = 0 excludes those rows entirely.

plot_weights: boolean, whether to plot distribution of weights, default False enforce: boolean, whether to force biases, default False replace: boolean, whether sampling will be done with replacement, default False.

When True, the enforce capacity check (qualifying rows >= n) is skipped because sampling n items with replacement from fewer qualifying rows is valid.

Returns: Pandas Series of normalized row weights.

obsidian.experiment.sampling.sample_with_bias(df, n, replace=False, seed=None, bias=None, enforce=False, plot_weights=False)[source]#

Returns a random Pandas DataFrame sample of data points from a population with or without bias.

df: DataFrame of candidates n: int, size of the design to pick replace: boolean, allow or disallow sampling from the same row more than once, default False bias: dictionary of biases in the format : {“column”: [lower_bound, upper_bound, relative_weight]}, default None

  • Weight >1 increases sampling probability for in-range rows.

  • Weight <1 decreases it.

  • Weight = 0 excludes those rows entirely.

enforce: boolean, whether to force biases, default False plot_weights: boolean, whether to plot distribution of weights, default False

Returns: Pandas DataFrame of sampled data points.