sampling.generate_lhs
generate_lhs(n, ranges, seed=None)Generates a Latin Hypercube Sample and scales it to the provided variable bounds.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| n | int | The total number of samples to generate. | required |
| ranges | Union[pd.DataFrame, Dict] | Definition of input variables. - Dict Format: {‘Name’: (Min, Max), …} e.g. {‘Length’: (0, 10)} - DataFrame Format: Columns [‘Name’, ‘Min’, ‘Max’] | required |
| seed | int | Sets the random seed for reproducibility. Defaults to None. | None |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | pd.DataFrame: A dataframe containing the scaled simulation parameters, where column names correspond to the keys in ranges. Returns an empty DataFrame if ranges is empty. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | If required columns are missing, types are incorrect, or Min >= Max. | |
| TypeError | If ranges is not a Dictionary or DataFrame. |
Examples
Using a dictionary (Recommended):
ranges = {'Length': (0, 10), 'Angle': (0, 90)}
df = generate_lhs(n=3, ranges=ranges, seed=42)
print(df.round(2))
# Length Angle
# 0 3.75 85.54
# 1 9.51 13.56
# 2 7.32 54.17Using a DataFrame (Legacy/Advanced):
import pandas as pd
vars_df = pd.DataFrame([
{'Name': 'Length', 'Min': 0, 'Max': 10},
{'Name': 'Angle', 'Min': 0, 'Max': 90}
])
df = generate_lhs(n=3, ranges=vars_df, seed=42)