def test_plot_partial_dependence(): """ Tests :func:`fatf.vis.feature_influence.plot_partial_dependence` function. """ feature_name = 'some feature' class_index = 1 class_name = 'middle' figure, axis = fvfi.plot_partial_dependence(FAKE_PD_ARRAY, FAKE_LINESPACE, class_index, feature_name, class_name) assert isinstance(figure, plt.Figure) p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( axis) # ...check title assert p_title == 'Partial Dependence' # ...check x range assert np.array_equal(p_x_range, [FAKE_LINESPACE[0], FAKE_LINESPACE[-1]]) # ...check x label assert p_x_label == feature_name # ...check y range assert np.array_equal(p_y_range, [-0.05, 1.05]) # ...check y label assert p_y_label == '{} class probability'.format(class_name) # Test the line assert len(axis.lines) == 1 l_data, l_colour, l_alpha, l_label, l_width = futv.get_line_data( axis.lines[0]) line_data = np.stack([FAKE_LINESPACE, FAKE_PD_ARRAY[:, class_index]], axis=1) assert np.array_equal(l_data, line_data) assert l_colour == 'lightsalmon' assert l_alpha == 0.6 assert l_label == 'PD' assert l_width == 7 # Validate plot legend legend = [ i for i in axis.get_children() if isinstance(i, matplotlib.legend.Legend) ] assert len(legend) == 1 legend_texts = legend[0].get_texts() assert len(legend_texts) == 1 assert legend_texts[0].get_text() == 'PD'
dp_to_explain_class_index, class_name=dp_to_explain_class, feature_name=selected_feature_name) ############################################################################### # ICE calculation is a base of Partial Dependence (:func:`fatf.transparency.\ # models.feature_influence.partial_dependence_ice`). Then, Partial Dependence # can be plotted either as an overlay on top of the ICE plot or on its own. # Calculate Partial Dependence pd_array = fatf_fi.partial_dependence_ice(ice_array) # Plot Partial Dependence on its own pd_plot_clean = fatf_vis_fi.plot_partial_dependence( pd_array, ice_linspace, explanation_class, class_name=explanation_class_name, feature_name=selected_feature_name) # Create a fresh ICE plot and add the Partial Dependence plot on top of it ice_plot_2 = fatf_vis_fi.plot_individual_conditional_expectation( ice_array, ice_linspace, explanation_class, class_name=explanation_class_name, feature_name=selected_feature_name) ice_plot_2_figure, ice_plot_2_axis = ice_plot_2 pd_plot_overlay = fatf_vis_fi.plot_partial_dependence( pd_array, ice_linspace, explanation_class,
def test_ice_pd_overlay(): """ Tests overlaying PD plot on top of an ICE plot. """ f_name = 'some feature' c_index = 1 c_name = 'middle' figure, axis = fvfi.plot_individual_conditional_expectation( FAKE_ICE_ARRAY, FAKE_LINESPACE, c_index, f_name, c_name) assert isinstance(figure, plt.Figure) assert isinstance(axis, plt.Axes) none, axis = fvfi.plot_partial_dependence(FAKE_PD_ARRAY, FAKE_LINESPACE, c_index, f_name, c_name, axis) assert none is None assert isinstance(axis, plt.Axes) # Inspect the canvas p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( axis) # ...check title assert p_title == ('Individual Conditional Expectation &\nPartial ' 'Dependence') # ...check x range assert np.array_equal(p_x_range, [FAKE_LINESPACE[0], FAKE_LINESPACE[-1]]) # ...check x label assert p_x_label == f_name # ...check y range assert np.array_equal(p_y_range, [-0.05, 1.05]) # ...check y label assert p_y_label == '{} class probability'.format(c_name) # Check ICE assert len(axis.collections) == 1 l_data, l_colour, l_alpha, l_label, l_width = futv.get_line_data( axis.collections[0], is_collection=True) assert len(l_data) == FAKE_ICE_ARRAY.shape[0] for i, line_array in enumerate(l_data): line_data = np.stack([FAKE_LINESPACE, FAKE_ICE_ARRAY[i, :, c_index]], axis=1) assert np.array_equal(line_array, line_data) assert np.isclose(l_colour, np.array([[0.412, 0.412, 0.412, 0.5]]), atol=1e-2).all() # dimgray mapping apparently assert l_alpha == 0.5 assert l_label == 'ICE' assert l_width == 1.75 # Check PD assert len(axis.lines) == 1 l_data, l_colour, l_alpha, l_label, l_width = futv.get_line_data( axis.lines[0]) line_data = np.stack([FAKE_LINESPACE, FAKE_PD_ARRAY[:, c_index]], axis=1) assert np.array_equal(l_data, line_data) assert l_colour == 'lightsalmon' assert l_alpha == 0.6 assert l_label == 'PD' assert l_width == 7 # Validate plot legend legend = [ i for i in axis.get_children() if isinstance(i, matplotlib.legend.Legend) ] assert len(legend) == 1 legend_texts = legend[0].get_texts() assert len(legend_texts) == 2 assert legend_texts[0].get_text() == 'PD' assert legend_texts[1].get_text() == 'ICE'
# Train a model clf = fatf_models.KNN() clf.fit(iris_X, 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, selected_feature_name)) # 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, explanation_class_name)) # 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( pd_array, pd_linspace, explanation_class, class_name=explanation_class_name, feature_name=selected_feature_name)