Using Partial Dependence ExplainerΒΆ

This example illustrates how to use the Partial Dependence (PD) explainer and its plotting function. This example only shows how to get the PD array straight from the data. The calculation of PD required to compute the Individual Conditional Expectation (ICE) as part of the process. By using the fatf.transparency.models.feature_influence.partial_dependence function the ICE array is computed, however it is never returned back to the user. If you want to inspect both ICE and PD then please have a look at the Using Partial Dependence Explainer example and the two following functions:



Explaining feature (index: 1): sepal width (cm).
Explaining class (index: 2): virginica.

# Author: Kacper Sokol <>
# License: new BSD

import as fatf_datasets
import fatf.utils.models as fatf_models

import fatf.transparency.models.feature_influence as fatf_fi

import fatf.vis.feature_influence as fatf_vis_fi


# Load data
iris_data_dict = fatf_datasets.load_iris()
iris_X = iris_data_dict['data']
iris_y = iris_data_dict['target'].astype(int)
iris_feature_names = iris_data_dict['feature_names']
iris_class_names = iris_data_dict['target_names']

# Train a model
clf = fatf_models.KNN(), iris_y)

# Select a feature to be explained
selected_feature_index = 1
selected_feature_name = iris_feature_names[selected_feature_index]
print('Explaining feature (index: {}): {}.'.format(selected_feature_index,

# Select class for which the explanation will be produced
explanation_class = 2
explanation_class_name = iris_class_names[explanation_class]
print('Explaining class (index: {}): {}.'.format(explanation_class,

# Define the number of samples to be generated (granularity of the explanation)
linspace_samples = 25

# Calculate Partial Dependence
pd_array, pd_linspace = fatf_fi.partial_dependence(
    iris_X, clf, selected_feature_index, steps_number=linspace_samples)

# Plot Partial Dependence on its own
pd_plot_clean = fatf_vis_fi.plot_partial_dependence(

Total running time of the script: ( 0 minutes 13.931 seconds)

Gallery generated by Sphinx-Gallery