Intermediate

Model-Specific Explainability Methods

Learn feature importance for tree models, partial dependence plots, Individual Conditional Expectation (ICE) plots, and counterfactual explanations.

Feature Importance

Tree-based models (Random Forest, XGBoost, LightGBM) provide built-in feature importance scores based on how much each feature contributes to reducing impurity or loss across all splits.

Python — Feature importance comparison
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance

# Train model
model = RandomForestClassifier(n_estimators=200).fit(X_train, y_train)

# Built-in (impurity-based) importance
impurity_imp = model.feature_importances_

# Permutation importance (more reliable, model-agnostic)
perm_result = permutation_importance(
    model, X_test, y_test,
    n_repeats=30, random_state=42
)
perm_imp = perm_result.importances_mean

# Compare both methods
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
sorted_idx = np.argsort(impurity_imp)

axes[0].barh(range(len(sorted_idx)),
             impurity_imp[sorted_idx])
axes[0].set_yticks(range(len(sorted_idx)))
axes[0].set_yticklabels(feature_names[sorted_idx])
axes[0].set_title("Impurity-based Importance")

sorted_idx2 = np.argsort(perm_imp)
axes[1].barh(range(len(sorted_idx2)),
             perm_imp[sorted_idx2])
axes[1].set_yticks(range(len(sorted_idx2)))
axes[1].set_yticklabels(feature_names[sorted_idx2])
axes[1].set_title("Permutation Importance")
plt.tight_layout()
plt.show()
Impurity importance is biased: Built-in feature importance from tree models is biased toward high-cardinality features (features with many unique values). Always prefer permutation importance for reliable feature ranking.

Partial Dependence Plots (PDP)

PDPs show the marginal effect of a feature on the predicted outcome, averaged across all samples. They reveal the relationship between a feature and the prediction while averaging out the effects of other features.

Python — Partial dependence plots
from sklearn.inspection import PartialDependenceDisplay

# One-way PDP
fig, ax = plt.subplots(figsize=(10, 6))
PartialDependenceDisplay.from_estimator(
    model, X_train,
    features=[0, 1, 2, 3],           # feature indices
    feature_names=feature_names,
    ax=ax
)
plt.suptitle("Partial Dependence Plots")
plt.tight_layout()
plt.show()

# Two-way PDP (interaction between two features)
fig, ax = plt.subplots(figsize=(8, 6))
PartialDependenceDisplay.from_estimator(
    model, X_train,
    features=[(0, 1)],                # pair of features
    feature_names=feature_names,
    ax=ax,
    kind='average'
)
plt.title("2D Partial Dependence")
plt.show()

ICE Plots

Individual Conditional Expectation (ICE) plots show the effect of a feature on the prediction for each individual sample, rather than averaging. They reveal heterogeneity that PDPs hide.

Python — ICE plots
from sklearn.inspection import PartialDependenceDisplay

fig, ax = plt.subplots(figsize=(10, 6))
PartialDependenceDisplay.from_estimator(
    model, X_train,
    features=[0],
    feature_names=feature_names,
    kind='both',          # 'both' shows ICE lines + PDP average
    centered=True,        # Center ICE lines at first value
    ax=ax
)
plt.title("ICE Plot with PDP overlay")
plt.show()

Counterfactual Explanations

Counterfactuals answer: "What would need to change for the prediction to be different?" They are intuitive for end users because they describe actionable changes.

Python — Counterfactual with DiCE
import dice_ml

# Create data and model interfaces
data = dice_ml.Data(
    dataframe=df,
    continuous_features=['age', 'income', 'credit_score'],
    outcome_name='loan_approved'
)
ml_model = dice_ml.Model(model=model, backend='sklearn')

# Generate counterfactuals
explainer = dice_ml.Dice(data, ml_model, method='random')
counterfactuals = explainer.generate_counterfactuals(
    query_instance=df.iloc[[rejected_applicant]],
    total_CFs=3,            # generate 3 alternatives
    desired_class="opposite"
)

# Display: "If your income were $55k instead of $45k
# and credit score were 720 instead of 680,
# the loan would be approved."
counterfactuals.visualize_as_dataframe()
Counterfactuals in practice: Counterfactual explanations are particularly powerful in consumer-facing applications. Instead of saying "you were rejected because of factors X, Y, Z," they say "here's what you could change to get approved." This is actionable and compliant with right-to-explanation regulations.

Method Selection Guide

GoalMethodScope
Rank features globallyPermutation importance, SHAP bar plotGlobal
Understand feature effectsPDP, ICE plotsGlobal
Explain one predictionSHAP force plot, LIME, counterfactualsLocal
Detect interactions2D PDP, SHAP dependence plotGlobal
Actionable adviceCounterfactual explanationsLocal