pdstools.explanations.Plots¶
Classes¶
Module Contents¶
- class Plots(explanations: pdstools.explanations.Explanations.Explanations)¶
Bases:
pdstools.utils.namespaces.LazyNamespace- Parameters:
explanations (pdstools.explanations.Explanations.Explanations)
- dependencies = ['numpy', 'plotly']¶
- dependency_group = 'explanations'¶
- X_AXIS_TITLE_DEFAULT = 'Contribution'¶
- Y_AXIS_TITLE_DEFAULT = 'Predictor'¶
- explanations¶
- aggregate¶
- contributions(top_n: int = defaults.top_n, top_k: int = defaults.top_k, descending: bool = defaults.descending, missing: bool = defaults.missing, remaining: bool = defaults.remaining, sort_by: str = defaults.sort_by.value, display_by: str = defaults.display_by.value)¶
Plots contributions for the overall model or a selected context.
- Args:
- top_n (int):
Number of top predictors to display.
- top_k (int):
Number of top unique values for each categorical predictor to display.
- descending (bool):
Whether to sort the predictors by most or least contribution.
- missing (bool):
Whether to include missing values in the plot.
- remaining (bool):
predictors/predictor values not included in the top_n/top_k will be grouped into a “remaining” category.
- sort_by (str):
Type of contribution calculation to use for sorting/selecting top predictors. Default is contribution_abs to select most impactful predictors.
- display_by (str):
Type of contribution calculation to display in plots. Default is contribution to show signed contribution values.
- Returns:
- tuple[go.Figure, list[go.Figure]]:
left: context header if context is selected, otherwise None
right: overall contributions plot and a list of predictor contribution plots.
- plot_contributions_for_overall(top_n: int = defaults.top_n, top_k: int = defaults.top_k, descending: bool = defaults.descending, missing: bool = defaults.missing, remaining: bool = defaults.remaining, sort_by: str = defaults.sort_by.value, display_by: str = defaults.display_by.value) tuple[plotly.graph_objects.Figure, list[plotly.graph_objects.Figure]]¶
- plot_contributions_by_context(context: dict[str, str], top_n: int = defaults.top_n, top_k: int = defaults.top_k, descending: bool = defaults.descending, missing: bool = defaults.missing, remaining: bool = defaults.remaining, sort_by: str = defaults.sort_by.value, display_by: str = defaults.display_by.value) tuple[plotly.graph_objects.Figure, plotly.graph_objects.Figure, list[plotly.graph_objects.Figure]]¶
- static _build_hover_customdata(df: polars.DataFrame, x_col: str)¶
Build customdata array and hovertemplate for contribution plots.
Expects df to contain a
frequency_pctcolumn.Returns (customdata, hovertemplate) with columns: predictor_name, predictor_type, contribution (x_col value), frequency_pct.
- Parameters:
df (polars.DataFrame)
x_col (str)
- _plot_overall_contributions(df: polars.DataFrame, x_col: str, y_col: str, x_title: str = X_AXIS_TITLE_DEFAULT, y_title: str = Y_AXIS_TITLE_DEFAULT, context: pdstools.explanations.ExplanationsUtils.ContextInfo | None = None) plotly.graph_objects.Figure¶
- Parameters:
df (polars.DataFrame)
x_col (str)
y_col (str)
x_title (str)
y_title (str)
context (pdstools.explanations.ExplanationsUtils.ContextInfo | None)
- Return type:
plotly.graph_objects.Figure
- _plot_predictor_contributions(df: polars.DataFrame, x_col: str, y_col: str, x_title: str = X_AXIS_TITLE_DEFAULT, y_title: str = Y_AXIS_TITLE_DEFAULT) list[plotly.graph_objects.Figure]¶
- static _plot_context_table(context_info: pdstools.explanations.ExplanationsUtils.ContextInfo) plotly.graph_objects.Figure¶
- Parameters:
context_info (pdstools.explanations.ExplanationsUtils.ContextInfo)
- Return type:
plotly.graph_objects.Figure