anchors/limes/lime_tabular.py
def check_stability(self,
                        data_row,
                        predict_fn,
                        labels=(1,),
                        top_labels=None,
                        num_features=10,
                        num_samples=5000,
                        distance_metric='euclidean',
                        model_regressor=None,
                        n_calls=10,
                        index_verbose=False,
                        verbose=False,
                        ls=False,
                        ape=None,
                        instances_in_sphere=[]):

        """
        Method to calculate stability indices for a trained LIME instance.
        The stability indices are relative to the particular data point we are explaining.
        The stability indices are described in the paper:
        "Statistical stability indices for LIME: obtaining reliable explanations for Machine Learning models".
        It can be found in the ArXiv online repository: https://arxiv.org/abs/2001.11757
        The paper is currently under review in the Journal of Operational Research Society (JORS)
        Args:
            data_row: 1d numpy array or scipy.sparse matrix, corresponding to a row
            predict_fn: prediction function. For classifiers, this should be a
                function that takes a numpy array and outputs prediction
                probabilities. For regressors, this takes a numpy array and
                returns the predictions. For ScikitClassifiers, this is
                `classifier.predict_proba()`. For ScikitRegressors, this
                is `regressor.predict()`. The prediction function needs to work
                on multiple feature vectors (the vectors randomly perturbed
                from the data_row).
            labels: iterable with labels to be explained.
            top_labels: if not None, ignore labels and produce explanations for
                the K labels with highest prediction probabilities, where K is
                this parameter.
            num_features: maximum number of features present in explanation
            num_samples: size of the neighborhood to learn the linear model
            distance_metric: the distance metric to use for weights.
            model_regressor: sklearn regressor to use in explanation. Defaults
                to Ridge regression in LimeBase.
                If not Ridge Regression, the stability indices may not be calculated
                and the method will raise a LocalModelError.
            n_calls: number of repeated Lime calls to perform.
                High number of calls slows down the execution,
                however a meaningful comparison takes place only when
                there is a reasonable number of calls to compare.
            index_verbose: Controls for the verbosity at the stability indices level,
                when set to True gives information about partial values related to stability.
            verbose: Controls for the verbosity at the LocalModel level,
                when set to True, gives information about the repeated calls of WRR.
        Returns:
            csi: index to evaluate the stability of the coefficients of each variable across
            different Lime explanations obtained from the repeated n_calls.
            Ranges from 0 to 100.
            vsi: index to evaluate whether the variables retrieved in different Lime explanations
                are the same. Ranges from 0 to 100.
        """

        # Override verbosity in the LimeBaseOvr instance
        previous_verbose = self.base.verbose
        self.base.verbose = verbose

        confidence_intervals = []
        print("searching for vsi and csi indicators...")
        for i in range(n_calls):
            if ls :
                alpha = self.explain_instance_training_dataset(data_row,
                                  predict_fn,
                                  labels,
                                  top_labels,
                                  num_features,
                                  num_samples,
                                  distance_metric,
                                  model_regressor,
                                  instances_in_sphere=instances_in_sphere,
                                  ape=ape,
                                  stability=True).easy_model.alpha
            else:
                alpha = self.explain_instance(data_row,
                                  predict_fn,
                                  labels,
                                  top_labels,
                                  num_features,
                                  num_samples,
                                  distance_metric,
                                  model_regressor,
                                  stability=True).easy_model.alpha

            # The first time check if the local model is WRR, otherwise raise Exception
            if not i:
                if alpha is None:
                    raise LocalModelError("""Lime Local Model is not a Weighted Ridge Regression (WRR),
                    Stability indices may not be computed: the formula is model specific""")
            
            confidence_intervals.append(self.confidence_intervals())

        csi, vsi = compare_confints(confidence_intervals=confidence_intervals,
                                    index_verbose=index_verbose)

        # Set back the original verbosity
        self.base.verbose = previous_verbose

        return csi, vsi

        Method to calculate stability indices for a trained LIME instance.
        The stability indices are relative to the particular data point we are explaining.
        The stability indices are described in the paper:
        "Statistical stability indices for LIME: obtaining reliable explanations for Machine Learning models".
        It can be found in the ArXiv online repository: https://arxiv.org/abs/2001.11757
        The paper is currently under review in the Journal of Operational Research Society (JORS)
        Args:
            data_row: 1d numpy array or scipy.sparse matrix, corresponding to a row
            predict_fn: prediction function. For classifiers, this should be a
                function that takes a numpy array and outputs prediction
                probabilities. For regressors, this takes a numpy array and
                returns the predictions. For ScikitClassifiers, this is
                `classifier.predict_proba()`. For ScikitRegressors, this
                is `regressor.predict()`. The prediction function needs to work
                on multiple feature vectors (the vectors randomly perturbed
                from the data_row).
            labels: iterable with labels to be explained.
            top_labels: if not None, ignore labels and produce explanations for
                the K labels with highest prediction probabilities, where K is
                this parameter.
            num_features: maximum number of features present in explanation
            num_samples: size of the neighborhood to learn the linear model
            distance_metric: the distance metric to use for weights.
            model_regressor: sklearn regressor to use in explanation. Defaults
                to Ridge regression in LimeBase.
                If not Ridge Regression, the stability indices may not be calculated
                and the method will raise a LocalModelError.
            n_calls: number of repeated Lime calls to perform.
                High number of calls slows down the execution,
                however a meaningful comparison takes place only when
                there is a reasonable number of calls to compare.
            index_verbose: Controls for the verbosity at the stability indices level,
                when set to True gives information about partial values related to stability.
            verbose: Controls for the verbosity at the LocalModel level,
                when set to True, gives information about the repeated calls of WRR.
        Returns:
            csi: index to evaluate the stability of the coefficients of each variable across
            different Lime explanations obtained from the repeated n_calls.
            Ranges from 0 to 100.
            vsi: index to evaluate whether the variables retrieved in different Lime explanations
                are the same. Ranges from 0 to 100.
        
Method to calculate stability indices for a trained LIME instance.
The stability indices are relative to the particular data point we are explaining.
The stability indices are described in the paper:
"Statistical stability indices for LIME: obtaining reliable explanations for Machine Learning models".
Method to calculate stability indices for a trained LIME instance . The stability indices are relative to the particular data point we are explaining . The stability indices are described in the paper : " Statistical stability indices for LIME : obtaining reliable explanations for Machine Learning models " .
def check_stability(self,
                        data_row,
                        predict_fn,
                        labels=(1,),
                        top_labels=None,
                        num_features=10,
                        num_samples=5000,
                        distance_metric='euclidean',
                        model_regressor=None,
                        n_calls=10,
                        index_verbose=False,
                        verbose=False,
                        ls=False,
                        ape=None,
                        instances_in_sphere=[]):
        previous_verbose = self.base.verbose
        self.base.verbose = verbose
        confidence_intervals = []
        print("searching for vsi and csi indicators...")
        for i in range(n_calls):
            if ls :
                alpha = self.explain_instance_training_dataset(data_row,
                                  predict_fn,
                                  labels,
                                  top_labels,
                                  num_features,
                                  num_samples,
                                  distance_metric,
                                  model_regressor,
                                  instances_in_sphere=instances_in_sphere,
                                  ape=ape,
                                  stability=True).easy_model.alpha
            else:
                alpha = self.explain_instance(data_row,
                                  predict_fn,
                                  labels,
                                  top_labels,
                                  num_features,
                                  num_samples,
                                  distance_metric,
                                  model_regressor,
                                  stability=True).easy_model.alpha
            if not i:
                if alpha is None:
                    raise LocalModelError("""Lime Local Model is not a Weighted Ridge Regression (WRR),
                    Stability indices may not be computed: the formula is model specific""")
            confidence_intervals.append(self.confidence_intervals())
        csi, vsi = compare_confints(confidence_intervals=confidence_intervals,
                                    index_verbose=index_verbose)
        self.base.verbose = previous_verbose
        return csi, vsi
def check_stability ( self , data_row , predict_fn , labels = ( 1 , ) , top_labels = None , num_features = 10 , num_samples = 5000 , distance_metric = 'euclidean' , model_regressor = None , n_calls = 10 , index_verbose = False , verbose = False , ls = False , ape = None , instances_in_sphere = [ ] ) : previous_verbose = self . base . verbose self . base . verbose = verbose confidence_intervals = [ ] print ( "searching for vsi and csi indicators..." ) for i in range ( n_calls ) : if ls : alpha = self . explain_instance_training_dataset ( data_row , predict_fn , labels , top_labels , num_features , num_samples , distance_metric , model_regressor , instances_in_sphere = instances_in_sphere , ape = ape , stability = True ) . easy_model . alpha else : alpha = self . explain_instance ( data_row , predict_fn , labels , top_labels , num_features , num_samples , distance_metric , model_regressor , stability = True ) . easy_model . alpha if not i : if alpha is None : raise LocalModelError ( """Lime Local Model is not a Weighted Ridge Regression (WRR),
                    Stability indices may not be computed: the formula is model specific""" ) confidence_intervals . append ( self . confidence_intervals ( ) ) csi , vsi = compare_confints ( confidence_intervals = confidence_intervals , index_verbose = index_verbose ) self . base . verbose = previous_verbose return csi , vsi
Method to calculate stability indices for a trained LIME instance.
Method to calculate stability indices for a trained LIME instance .
"""
        Method to calculate stability indices for a trained LIME instance.
        The stability indices are relative to the particular data point we are explaining.
        The stability indices are described in the paper:
        "Statistical stability indices for LIME: obtaining reliable explanations for Machine Learning models".
        It can be found in the ArXiv online repository: https://arxiv.org/abs/2001.11757
        The paper is currently under review in the Journal of Operational Research Society (JORS)
        Args:
            data_row: 1d numpy array or scipy.sparse matrix, corresponding to a row
            predict_fn: prediction function. For classifiers, this should be a
                function that takes a numpy array and outputs prediction
                probabilities. For regressors, this takes a numpy array and
                returns the predictions. For ScikitClassifiers, this is
                `classifier.predict_proba()`. For ScikitRegressors, this
                is `regressor.predict()`. The prediction function needs to work
                on multiple feature vectors (the vectors randomly perturbed
                from the data_row).
            labels: iterable with labels to be explained.
            top_labels: if not None, ignore labels and produce explanations for
                the K labels with highest prediction probabilities, where K is
                this parameter.
            num_features: maximum number of features present in explanation
            num_samples: size of the neighborhood to learn the linear model
            distance_metric: the distance metric to use for weights.
            model_regressor: sklearn regressor to use in explanation. Defaults
                to Ridge regression in LimeBase.
                If not Ridge Regression, the stability indices may not be calculated
                and the method will raise a LocalModelError.
            n_calls: number of repeated Lime calls to perform.
                High number of calls slows down the execution,
                however a meaningful comparison takes place only when
                there is a reasonable number of calls to compare.
            index_verbose: Controls for the verbosity at the stability indices level,
                when set to True gives information about partial values related to stability.
            verbose: Controls for the verbosity at the LocalModel level,
                when set to True, gives information about the repeated calls of WRR.
        Returns:
            csi: index to evaluate the stability of the coefficients of each variable across
            different Lime explanations obtained from the repeated n_calls.
            Ranges from 0 to 100.
            vsi: index to evaluate whether the variables retrieved in different Lime explanations
                are the same. Ranges from 0 to 100.
        """ # Override verbosity in the LimeBaseOvr instance # The first time check if the local model is WRR, otherwise raise Exception # Set back the original verbosity
index to evaluate the stability of the coefficients of each variable across
different Lime explanations obtained from the repeated n_calls.
Ranges from 0 to 100.
vsi: index to evaluate whether the variables retrieved in different Lime explanations
are the same. Ranges from 0 to 100.
index to evaluate the stability of the coefficients of each variable across different Lime explanations obtained from the repeated n_calls . Ranges from 0 to 100 . vsi : index to evaluate whether the variables retrieved in different Lime explanations are the same . Ranges from 0 to 100 .
csi
self
data_row
1d numpy array or scipy.sparse matrix, corresponding to a row
1d numpy array or scipy . sparse matrix corresponding to a row
predict_fn
prediction function. For classifiers, this should be a
function that takes a numpy array and outputs prediction
probabilities. For regressors, this takes a numpy array and
returns the predictions. For ScikitClassifiers, this is
`classifier.predict_proba()`. For ScikitRegressors, this
is `regressor.predict()`. The prediction function needs to work
on multiple feature vectors (the vectors randomly perturbed
from the data_row).
prediction function . For classifiers this should be a function that takes a numpy array and outputs prediction probabilities . For regressors this takes a numpy array and returns the predictions . For ScikitClassifiers this is ` classifier . predict_proba () ` . For ScikitRegressors this is ` regressor . predict () ` . The prediction function needs to work on multiple feature vectors ( the vectors randomly perturbed from the data_row ) .
labels
iterable with labels to be explained.
iterable with labels to be explained .
top_labels
if not None, ignore labels and produce explanations for
the K labels with highest prediction probabilities, where K is
this parameter.
if not None ignore labels and produce explanations for the K labels with highest prediction probabilities where K is this parameter .
num_features
maximum number of features present in explanation
maximum number of features present in explanation
num_samples
size of the neighborhood to learn the linear model
size of the neighborhood to learn the linear model
distance_metric
the distance metric to use for weights.
the distance metric to use for weights .
model_regressor
sklearn regressor to use in explanation. Defaults
to Ridge regression in LimeBase.
If not Ridge Regression, the stability indices may not be calculated
and the method will raise a LocalModelError.
sklearn regressor to use in explanation . Defaults to Ridge regression in LimeBase . If not Ridge Regression the stability indices may not be calculated and the method will raise a LocalModelError .
n_calls
number of repeated Lime calls to perform.
High number of calls slows down the execution,
however a meaningful comparison takes place only when
there is a reasonable number of calls to compare.
number of repeated Lime calls to perform . High number of calls slows down the execution however a meaningful comparison takes place only when there is a reasonable number of calls to compare .
index_verbose
Controls for the verbosity at the stability indices level,
when set to True gives information about partial values related to stability.
Controls for the verbosity at the stability indices level when set to True gives information about partial values related to stability .
verbose
Controls for the verbosity at the LocalModel level,
when set to True, gives information about the repeated calls of WRR.
Controls for the verbosity at the LocalModel level when set to True gives information about the repeated calls of WRR .
ls
ape
instances_in_sphere