ALE#
- class hidimstat.visualization.ALE(estimator, feature_names=None)[source]#
Bases:
objectAccumulated Local Effect (ALE) visualisation. Apley and Zhu[1]
ALE measures how the predictions of a model change on average when a feature varies locally within small intervals (bins). Unlike Partial Dependence Plots, ALE is not affected by feature correlations because it averages local differences rather than marginalising over the full feature distribution.
Formally, for a single continuous feature \(x_j\), the 1D ALE corresponds to Equation (7) from Apley and Zhu[1]:
(1)#\[\hat{f}_{j,\text{ALE}}(x) = \int_{z_{0,j}}^{x} \mathbb{E}\!\left[ \frac{\partial f(X)}{\partial X_j} \;\middle|\; X_j = z \right] dz\]The centered 1D ALE curve \(\hat{f}_{j,\text{ALE}}(x_j)\) is obtained by subtracting a constant \(c\) so its weighted mean over the training distribution is zero: \(\hat{f}_{j,\text{ALE}}(x_j) = g_{j,\text{ALE}}(x_j) - c\).
For a pair of continuous features \((x_j, x_l)\), the uncentered 2D ALE interaction corresponds to Equation (11) from Apley and Zhu[1]:
(2)#\[h_{\{j,l\},\text{ALE}}(x_j, x_l) = \int_{x_{\text{min},j}}^{x_j} \int_{x_{\text{min},l}}^{x_l} \mathbb{E}\!\left[ \frac{\partial^2 f(X)}{\partial X_j \partial X_l} \;\middle|\; X_j = z_j, X_l = z_l \right] dz_j dz_l\]The final 2D ALE effect \(f_{\{j,l\},\text{ALE}}(x_j, x_l)\) is then “doubly-centered” by subtracting the zero-order and first-order ALE main effects of \(X_j\) and \(X_l\).
- Parameters:
- estimatorfitted sklearn-compatible estimator
Must expose predict, predict_proba, or decision_function.
- feature_nameslist of str, optional
Names of the features. If None, X0, X1, … will be used.
Notes
Added in version 0.4.0.
Estimators and Discretization
In practice, the continuous derivatives and integrals are unknown. The package implements the local empirical estimators.
For the 1D ALE estimator (approximating (1) via Equation (9) of the reference paper): For each bin defined by the quantile grid of X[:, feature_idx], the local effect is estimated as the average difference in model output when the feature moves from the lower to the upper bin edge across all samples that fall within that bin. The cumulative sum of these average effects gives the (uncentered) ALE curve, which is then centered so its weighted mean is zero.
For the 2D ALE estimator (approximating (2) via Equation (14) of the reference paper): The feature space of the pair is partitioned into a 2D grid of rectangular bins. The local interaction effect within a bin is estimated using second-order differences across the four corners of the bin for all instances falling into it. The uncentered interaction surface is computed by accumulating these local differences across both axes before performing the double-centering transformation.
References
- plot(X, features, feature_type='auto', method='predict', grid_resolution='auto', percentiles=(5, 95), confidence_level=0.95, n_bootstraps=20, n_jobs=1, random_state=None, cmap='viridis', **kwargs)[source]#
Compute and display the ALE plot for one or two features.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Dataset used to build the quantile grid and to gather local effects.
- featuresint or list of int
Feature index (1D ALE) or pair of feature indices (2D ALE).
- feature_typestring among “auto”, “continuous”, or “categorical”
Specify the type of values the feature has for 1D ALE. Set by default to auto and in this case :
non-numeric feature : categorical
numeric feature : categorical if the feature has less than 10 unique values, and continuous otherwise
- methodstr, default=”predict”
The method to use for the prediction. Supported methods are “predict”, “predict_proba” or “decision_function”.
- grid_resolutionint, tuple of int, or “auto”, default=”auto”
Number of bins per feature axis. If a tuple, specifies the number of bins separately per feature axis for 2D ALE.
If “auto”, the number of bins is determined automatically to minimize the histogram error per feature axis, and scaled down to maintain data density in the 2D ALE case.
Note that the final number of bins in the quantile grids may be strictly less than grid_resolution (or the auto-calculated value) if the data contains many duplicate values or fewer unique points than requested.
- percentilestuple of float, default=(5, 95)
The lower and upper percentile used to create the extreme values for the grid. Must be in [0, 100].
- confidence_levelfloat, default=0.95
The confidence level used to compute the confidence intervals (e.g., 0.95 for 95%) for the 1D ALE curve. If set to 0, confidence intervals are not computed. Must be in [0, 1[.
- n_bootstrapsint, default=20
Number of bootstrap samples to generate if confidence_level is not 0.
- n_jobsint, default=1
Number of jobs to run in parallel during bootstrapping. -1 means using all processors.
- random_stateint or None, default=None
Seed for reproducible bootstraps.
- cmapstr, default=”viridis”
Matplotlib colormap used for the 2D mesh plot.
- **kwargs
Extra keyword arguments forwarded to:
sns.lineplot for 1D plots;
ax.pcolormesh for 2D plots.
- Returns:
- axesndarray of Axes
The matplotlib Axes objects that make up the figure.
Examples using hidimstat.visualization.ALE#
Visualization with Accumulated Local Effects (ALE)