sampling.effects
sampling.effects
Functions
| Name | Description |
|---|---|
| plot_all_partial_dependence | Generates Partial Dependence Plots (PDPs) for every feature in a DataFrame against a target variable, |
| randorient | Generates a random orientation of a sampling matrix. |
| screening_plot | Generates a plot with elementary effect screening metrics. |
| screening_print | Generates a DataFrame with elementary effect screening metrics. |
plot_all_partial_dependence
sampling.effects.plot_all_partial_dependence(
df,
df_target,
model='GradientBoostingRegressor',
nrows=5,
ncols=6,
figsize=(20, 15),
title='',
)Generates Partial Dependence Plots (PDPs) for every feature in a DataFrame against a target variable, arranged in a grid.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| df | pd.DataFrame | DataFrame containing the features. | required |
| df_target | pd.Series | Series containing the target variable. | required |
| model | str | Name of the model class to use (e.g., “GradientBoostingRegressor”). Defaults to “GradientBoostingRegressor”. | 'GradientBoostingRegressor' |
| nrows | int | Number of rows in the grid of subplots. Defaults to 5. | 5 |
| ncols | int | Number of columns in the grid of subplots. Defaults to 6. | 6 |
| figsize | tuple | Figure size (width, height) in inches. Defaults to (20, 15). | (20, 15) |
| title | str | Title for the subplots. Defaults to ““. | '' |
Returns
| Name | Type | Description |
|---|---|---|
| None | None |
Examples
>>> form spotpython.utils.effects import plot_all_partial_dependence
>>> from sklearn.datasets import load_boston
>>> import pandas as pd
>>> data = load_boston()
>>> df = pd.DataFrame(data.data, columns=data.feature_names)
>>> df_target = pd.Series(data.target, name="target")
>>> plot_all_partial_dependence(df, df_target, model="GradientBoostingRegressor", nrows=5, ncols=6, figsize=(20, 15))randorient
sampling.effects.randorient(k, p, xi, seed=None)Generates a random orientation of a sampling matrix. This function creates a random sampling matrix for a given number of dimensions (k), number of levels (p), and step length (xi). The resulting matrix is used for screening designs in the context of experimental design.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| k | int | Number of dimensions. | required |
| p | int | Number of levels. | required |
| xi | float | Step length. | required |
| seed | int | Seed for the random number generator. Defaults to None. | None |
Returns
| Name | Type | Description |
|---|---|---|
| np.ndarray | np.ndarray: A random sampling matrix of shape (k+1, k). |
Example
randorient(k=2, p=3, xi=0.5) array([[0. , 0. ], [0.5, 0.5], [1. , 1. ]])
screening_plot
sampling.effects.screening_plot(X, fun, xi, p, labels, bounds=None, show=True)Generates a plot with elementary effect screening metrics.
This function calculates the mean and standard deviation of the elementary effects for a given set of design variables and plots the results.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| X | np.ndarray | The screening plan matrix, typically structured within a [0,1]^k box. | required |
| fun | object | The objective function to evaluate at each design point in the screening plan. | required |
| xi | float | The elementary effect step length factor. | required |
| p | int | Number of discrete levels along each dimension. | required |
| labels | list of str | A list of variable names corresponding to the design variables. | required |
| bounds | np.ndarray | A 2xk matrix where the first row contains lower bounds and the second row contains upper bounds for each variable. | None |
| show | bool | If True, the plot is displayed. Defaults to True. | True |
Returns
| Name | Type | Description |
|---|---|---|
| None | None | The function generates a plot of the results. |
Examples
>>> import numpy as np
from spotpython.utils.effects import screening, screeningplan
from spotpython.fun.objectivefunctions import Analytical
fun = Analytical()
k = 10
p = 10
xi = 1
r = 25
X = screeningplan(k=k, p=p, xi=xi, r=r) # shape (r x (k+1), k)
# Provide real-world bounds from the wing weight docs (2 x 10).
value_range = np.array([
[150, 220, 6, -10, 16, 0.5, 0.08, 2.5, 1700, 0.025],
[200, 300, 10, 10, 45, 1.0, 0.18, 6.0, 2500, 0.08 ],
])
labels = [
"S_W", "W_fw", "A", "Lambda",
"q", "lambda", "tc", "N_z",
"W_dg", "W_p"
]
screening(
X=X,
fun=fun.fun_wingwt,
bounds=value_range,
xi=xi,
p=p,
labels=labels,
print=False,
)screening_print
sampling.effects.screening_print(X, fun, xi, p, labels, bounds=None)Generates a DataFrame with elementary effect screening metrics.
This function calculates the mean and standard deviation of the elementary effects for a given set of design variables and returns the results as a Pandas DataFrame.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| X | np.ndarray | The screening plan matrix, typically structured within a [0,1]^k box. | required |
| fun | object | The objective function to evaluate at each design point in the screening plan. | required |
| xi | float | The elementary effect step length factor. | required |
| p | int | Number of discrete levels along each dimension. | required |
| labels | list of str | A list of variable names corresponding to the design variables. | required |
| bounds | np.ndarray | A 2xk matrix where the first row contains lower bounds and the second row contains upper bounds for each variable. | None |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | pd.DataFrame: A DataFrame containing three columns: - ‘varname’: The name of each variable. - ‘mean’: The mean of the elementary effects for each variable. - ‘sd’: The standard deviation of the elementary effects for each variable. | |
| pd.DataFrame | or None: If print is set to False, a plot of the results is generated instead of returning a DataFrame. |
Examples
>>> import numpy as np
from spotpython.utils.effects import screening, screeningplan
from spotpython.fun.objectivefunctions import Analytical
fun = Analytical()
k = 10
p = 10
xi = 1
r = 25
X = screeningplan(k=k, p=p, xi=xi, r=r) # shape (r x (k+1), k)
# Provide real-world bounds from the wing weight docs (2 x 10).
value_range = np.array([
[150, 220, 6, -10, 16, 0.5, 0.08, 2.5, 1700, 0.025],
[200, 300, 10, 10, 45, 1.0, 0.18, 6.0, 2500, 0.08 ],
])
labels = [
"S_W", "W_fw", "A", "Lambda",
"q", "lambda", "tc", "N_z",
"W_dg", "W_p"
]
screening(
X=X,
fun=fun.fun_wingwt,
bounds=value_range,
xi=xi,
p=p,
labels=labels,
print=False,
)