PDP
- class ordinal_xai.interpretation.pdp.PDP(model: Any, X: DataFrame, y: Series | None = None)[source]
Bases:
BaseInterpretationPartial Dependence Plot (PDP) interpretation method.
This class implements Partial Dependence Plots for interpreting ordinal regression models. PDPs show how a feature affects predictions while accounting for the average effect of other features. This helps understand the relationship between features and predictions in a model-agnostic way.
- Parameters:
model (
ordinal_xai.models.base_model.BaseOrdinalModel) – The trained ordinal regression model to interpretX (pd.DataFrame) – Dataset used for interpretation, shape (n_samples, n_features)
y (Optional[pd.Series], default=None) – Target labels, shape (n_samples,)
- model
The trained model to interpret
- Type:
ordinal_xai.models.base_model.BaseOrdinalModel
- X
Dataset used for interpretation
- Type:
pd.DataFrame
- y
Target labels
- Type:
Optional[pd.Series]
- feature_names_
Names of features in the dataset
- Type:
List[str]
Notes
PDPs are global interpretation methods that show the average effect of a feature
The method works by varying one feature while keeping others at their observed values
For each feature value, predictions are made and averaged across all samples
This helps understand the marginal effect of features on predictions
- __init__(model: Any, X: DataFrame, y: Series | None = None)[source]
Initialize the PDP interpretation method.
- Parameters:
model (
ordinal_xai.models.base_model.BaseOrdinalModel) – The trained ordinal regression model to interpretX (pd.DataFrame) – Dataset used for interpretation, shape (n_samples, n_features)
y (Optional[pd.Series], default=None) – Target labels, shape (n_samples,)
- Raises:
ValueError – If X is not a DataFrame or y is not a Series
- explain(observation_idx: int | None = None, feature_subset: List[str] | List[int] | None = None, plot: bool = False, max_features_per_figure: int = 12) Dict[str, Dict[str, ndarray]][source]
Generate Partial Dependence Plots for the selected features.
This method computes and optionally visualizes the partial dependence of the model’s predictions on the selected features. For each feature, it shows how the average prediction changes as the feature value changes, while accounting for the effect of other features.
- Parameters:
observation_idx (Optional[int], default=None) – Ignored (PDP is a global method)
feature_subset (Optional[Union[List[str], List[int]]], default=None) – List of feature names or indices to plot. If None, all features are used
plot (bool, default=False) – Whether to create visualizations
max_features_per_figure (int, default=12) – Maximum number of features to display per figure (for large datasets)
- Returns:
Dictionary containing PDP results for each feature: - ‘grid_values’: Feature values used for plotting - ‘average’: Average predictions at each feature value
- Return type:
Dict[str, Dict[str, np.ndarray]]
- Raises:
ValueError – If feature_subset contains invalid feature names or indices
RuntimeError – If model is not fitted and cannot be fitted automatically
- _plot_pdps(results: Dict[str, Dict[str, ndarray]], feature_subset: List[str], max_features_per_figure: int = 12) None[source]
Create visualization of Partial Dependence Plots with pagination for large feature sets.
- Parameters:
results (Dict[str, Dict[str, np.ndarray]]) – PDP results for each feature
feature_subset (List[str]) – List of features to plot
max_features_per_figure (int, default=12) – Maximum number of features to display per figure
- _abc_impl = <_abc._abc_data object>