ape_tabular_experiments.py
def ape_illustrative_results(ape_tabular, instance, counterfactual_in_sphere):
    """
    Function that print the explanation of ape depending on the distribution of counterfactual instances located in the hyper field
    Args: ape_tabular: ape tabular object used to explain the target instance
          instance: Target instance to explain
          counterfactual_in_sphere: List of counterfactual instances located in the hyper field
    Return:
    """
    multimodal = ape_tabular.multimodal_results
    if multimodal:
        # In case of multimodal distribution we generate an anchor explanation with multiple clusters centers as counterfactual explanations
        anchor_exp = ape_tabular.anchor_explainer.explain_instance(instance, ape_tabular.black_box_predict, threshold=ape_tabular.threshold_precision, 
                                        delta=0.1, tau=0.15, batch_size=100, max_anchor_size=None, stop_on_first=False,
                                        desired_label=None, beam_size=4)
        # Generate rules and data frame for applying anchors 
        print("rule by anchor", anchor_exp.names())
        # Kelbow method to detect the best number of clusters
        visualizer = KElbowVisualizer(KMeans(), k=(1,8))
        x_elbow = np.array(counterfactual_in_sphere)
        visualizer.fit(x_elbow)
        n_clusters = visualizer.elbow_value_
        if n_clusters is not None:
            print("n CLUSTERS ", n_clusters) # Corresponding to the number of counterfactual instances generated by APE
            kmeans = KMeans(n_clusters=n_clusters)
            kmeans.fit(counterfactual_in_sphere)
            clusters_centers = kmeans.cluster_centers_
            print("Mean center of clusters from KMEANS ", clusters_centers)
    else:
        # In case of unimodal distribution we generate a linear explanation model based on local surrogate trained over the training dataset along with
        # the closest counterfactual corresponding to the center of the explantion model
        local_surrogate_exp = ape_tabular.lime_explainer.explain_instance_training_dataset(ape_tabular.closest_counterfactual, 
                                                                    ape_tabular.black_box_predict_proba, num_features=4)
        counter_factual_class = ape_tabular.black_box_predict(ape_tabular.closest_counterfactual.reshape(1,-1))[0]
        print('Local Surrogate explanation for %s' % ape_tabular.class_names[counter_factual_class])
        print('\n'.join(map(str, local_surrogate_exp.as_list())))
        print("Closest counterfactual:", ape_tabular.closest_counterfactual)
    return 0, 0, 0

    Function that print the explanation of ape depending on the distribution of counterfactual instances located in the hyper field
    Args: ape_tabular: ape tabular object used to explain the target instance
          instance: Target instance to explain
          counterfactual_in_sphere: List of counterfactual instances located in the hyper field
    Return:
    
Function that print the explanation of ape depending on the distribution of counterfactual instances located in the hyper field
Args: ape_tabular: ape tabular object used to explain the target instance
instance: Target instance to explain
counterfactual_in_sphere: List of counterfactual instances located in the hyper field
Return.
Function that print the explanation of ape depending on the distribution of counterfactual instances located in the hyper field Args : ape_tabular : ape tabular object used to explain the target instance instance : Target instance to explain counterfactual_in_sphere : List of counterfactual instances located in the hyper field Return .
def ape_illustrative_results(ape_tabular, instance, counterfactual_in_sphere):
    multimodal = ape_tabular.multimodal_results
    if multimodal:
        anchor_exp = ape_tabular.anchor_explainer.explain_instance(instance, ape_tabular.black_box_predict, threshold=ape_tabular.threshold_precision, 
                                        delta=0.1, tau=0.15, batch_size=100, max_anchor_size=None, stop_on_first=False,
                                        desired_label=None, beam_size=4)
        print("rule by anchor", anchor_exp.names())
        visualizer = KElbowVisualizer(KMeans(), k=(1,8))
        x_elbow = np.array(counterfactual_in_sphere)
        visualizer.fit(x_elbow)
        n_clusters = visualizer.elbow_value_
        if n_clusters is not None:
            print("n CLUSTERS ", n_clusters) 
            kmeans = KMeans(n_clusters=n_clusters)
            kmeans.fit(counterfactual_in_sphere)
            clusters_centers = kmeans.cluster_centers_
            print("Mean center of clusters from KMEANS ", clusters_centers)
    else:
        local_surrogate_exp = ape_tabular.lime_explainer.explain_instance_training_dataset(ape_tabular.closest_counterfactual, 
                                                                    ape_tabular.black_box_predict_proba, num_features=4)
        counter_factual_class = ape_tabular.black_box_predict(ape_tabular.closest_counterfactual.reshape(1,-1))[0]
        print('Local Surrogate explanation for %s' % ape_tabular.class_names[counter_factual_class])
        print('\n'.join(map(str, local_surrogate_exp.as_list())))
        print("Closest counterfactual:", ape_tabular.closest_counterfactual)
    return 0, 0, 0
def ape_illustrative_results ( ape_tabular , instance , counterfactual_in_sphere ) : multimodal = ape_tabular . multimodal_results if multimodal : anchor_exp = ape_tabular . anchor_explainer . explain_instance ( instance , ape_tabular . black_box_predict , threshold = ape_tabular . threshold_precision , delta = 0.1 , tau = 0.15 , batch_size = 100 , max_anchor_size = None , stop_on_first = False , desired_label = None , beam_size = 4 ) print ( "rule by anchor" , anchor_exp . names ( ) ) visualizer = KElbowVisualizer ( KMeans ( ) , k = ( 1 , 8 ) ) x_elbow = np . array ( counterfactual_in_sphere ) visualizer . fit ( x_elbow ) n_clusters = visualizer . elbow_value_ if n_clusters is not None : print ( "n CLUSTERS " , n_clusters ) kmeans = KMeans ( n_clusters = n_clusters ) kmeans . fit ( counterfactual_in_sphere ) clusters_centers = kmeans . cluster_centers_ print ( "Mean center of clusters from KMEANS " , clusters_centers ) else : local_surrogate_exp = ape_tabular . lime_explainer . explain_instance_training_dataset ( ape_tabular . closest_counterfactual , ape_tabular . black_box_predict_proba , num_features = 4 ) counter_factual_class = ape_tabular . black_box_predict ( ape_tabular . closest_counterfactual . reshape ( 1 , - 1 ) ) [ 0 ] print ( 'Local Surrogate explanation for %s' % ape_tabular . class_names [ counter_factual_class ] ) print ( '\n' . join ( map ( str , local_surrogate_exp . as_list ( ) ) ) ) print ( "Closest counterfactual:" , ape_tabular . closest_counterfactual ) return 0 , 0 , 0
Function that print the explanation of ape depending on the distribution of counterfactual instances located in the hyper field
Args: ape_tabular: ape tabular object used to explain the target instance
instance: Target instance to explain
counterfactual_in_sphere: List of counterfactual instances located in the hyper field
Return:
Function that print the explanation of ape depending on the distribution of counterfactual instances located in the hyper field Args : ape_tabular : ape tabular object used to explain the target instance instance : Target instance to explain counterfactual_in_sphere : List of counterfactual instances located in the hyper field Return :
"""
    Function that print the explanation of ape depending on the distribution of counterfactual instances located in the hyper field
    Args: ape_tabular: ape tabular object used to explain the target instance
          instance: Target instance to explain
          counterfactual_in_sphere: List of counterfactual instances located in the hyper field
    Return:
    """ # In case of multimodal distribution we generate an anchor explanation with multiple clusters centers as counterfactual explanations # Generate rules and data frame for applying anchors  # Kelbow method to detect the best number of clusters # Corresponding to the number of counterfactual instances generated by APE # In case of unimodal distribution we generate a linear explanation model based on local surrogate trained over the training dataset along with # the closest counterfactual corresponding to the center of the explantion model
ape_tabular
instance
counterfactual_in_sphere