class_docstr: Explains predictions on Image (i.e. matrix) data.
  For numerical features, perturb them by sampling from a Normal(0,1) and
  doing the inverse operation of mean-centering and scaling, according to the
  means and stds in the training data. For categorical features, perturb by
  sampling according to the training distribution, and making a binary
  feature that is 1 when the value is the same as the instance being
  explained.
class_docstr_tok: ["Explains","predictions","on","Image","(","i.e",".","matrix",")","data",".","For","numerical","features",",","perturb","them","by","sampling","from","a","Normal","(","0,1",")","and","doing","the","inverse","operation","of","mean-centering","and","scaling",",","according","to","the","means","and","stds","in","the","training","data",".","For","categorical","features",",","perturb","by","sampling","according","to","the","training","distribution",",","and","making","a","binary","feature","that","is","1","when","the","value","is","the","same","as","the","instance","being","explained","."]
docstr: Generates explanations for a prediction.
  
  First, we generate neighborhood data by randomly perturbing features
  from the instance (see __data_inverse). We then learn locally weighted
  linear models on this neighborhood data to explain each of the classes
  in an interpretable way (see lime_base.py).
  
  Args:
      image: 3 dimension RGB image. If this is only two dimensional,
          we will assume it's a grayscale image and call gray2rgb.
      classifier_fn: classifier prediction probability function, which
          takes a numpy array and outputs prediction probabilities.  For
          ScikitClassifiers , this is classifier.predict_proba.
      labels: iterable with labels to be explained.
      hide_color: If not None, will hide superpixels with this color.
          Otherwise, use the mean pixel color of the image.
      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
      batch_size: batch size for model predictions
      distance_metric: the distance metric to use for weights.
      model_regressor: sklearn regressor to use in explanation. Defaults
      to Ridge regression in LimeBase. Must have model_regressor.coef_
      and 'sample_weight' as a parameter to model_regressor.fit()
      segmentation_fn: SegmentationAlgorithm, wrapped skimage
      segmentation function
      random_seed: integer used as random seed for the segmentation
          algorithm. If None, a random integer, between 0 and 1000,
          will be generated using the internal random number generator.
      progress_bar: if True, show tqdm progress bar.
  
  Returns:
      An ImageExplanation object (see lime_image.py) with the corresponding
      explanations.
docstr_tok: ["Generates","explanations","for","a","prediction",".","First",",","we","generate","neighborhood","data","by","randomly","perturbing","features","from","the","instance","(","see","__data_inverse",")",".","We","then","learn","locally","weighted","linear","models","on","this","neighborhood","data","to","explain","each","of","the","classes","in","an","interpretable","way","(","see","lime_base.py",")",".","Args",":","image",":","3","dimension","RGB","image",".","If","this","is","only","two","dimensional",",","we","will","assume","it","'s","a","grayscale","image","and","call","gray2rgb",".","classifier_fn",":","classifier","prediction","probability","function",",","which","takes","a","numpy","array","and","outputs","prediction","probabilities",".","For","ScikitClassifiers",",","this","is","classifier.predict_proba",".","labels",":","iterable","with","labels","to","be","explained",".","hide_color",":","If","not","None",",","will","hide","superpixels","with","this","color",".","Otherwise",",","use","the","mean","pixel","color","of","the","image",".","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","batch_size",":","batch","size","for","model","predictions","distance_metric",":","the","distance","metric","to","use","for","weights",".","model_regressor",":","sklearn","regressor","to","use","in","explanation",".","Defaults","to","Ridge","regression","in","LimeBase",".","Must","have","model_regressor.coef_","and","'sample_weight","'","as","a","parameter","to","model_regressor.fit","(",")","segmentation_fn",":","SegmentationAlgorithm",",","wrapped","skimage","segmentation","function","random_seed",":","integer","used","as","random","seed","for","the","segmentation","algorithm",".","If","None",",","a","random","integer",",","between","0","and","1000",",","will","be","generated","using","the","internal","random","number","generator",".","progress_bar",":","if","True",",","show","tqdm","progress","bar",".","Returns",":","An","ImageExplanation","object","(","see","lime_image.py",")","with","the","corresponding","explanations","."]
code: def explain_instance(
      self,
      image,
      classifier_fn,
      labels=(1,),
      hide_color=None,
      top_labels=5,
      num_features=100000,
      num_samples=1000,
      batch_size=10,
      segmentation_fn=None,
      distance_metric="cosine",
      model_regressor=None,
      random_seed=None,
      progress_bar=True,
  ):
      """Generates explanations for a prediction.
  
      First, we generate neighborhood data by randomly perturbing features
      from the instance (see __data_inverse). We then learn locally weighted
      linear models on this neighborhood data to explain each of the classes
      in an interpretable way (see lime_base.py).
  
      Args:
          image: 3 dimension RGB image. If this is only two dimensional,
              we will assume it's a grayscale image and call gray2rgb.
          classifier_fn: classifier prediction probability function, which
              takes a numpy array and outputs prediction probabilities.  For
              ScikitClassifiers , this is classifier.predict_proba.
          labels: iterable with labels to be explained.
          hide_color: If not None, will hide superpixels with this color.
              Otherwise, use the mean pixel color of the image.
          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
          batch_size: batch size for model predictions
          distance_metric: the distance metric to use for weights.
          model_regressor: sklearn regressor to use in explanation. Defaults
          to Ridge regression in LimeBase. Must have model_regressor.coef_
          and 'sample_weight' as a parameter to model_regressor.fit()
          segmentation_fn: SegmentationAlgorithm, wrapped skimage
          segmentation function
          random_seed: integer used as random seed for the segmentation
              algorithm. If None, a random integer, between 0 and 1000,
              will be generated using the internal random number generator.
          progress_bar: if True, show tqdm progress bar.
  
      Returns:
          An ImageExplanation object (see lime_image.py) with the corresponding
          explanations.
      """
      if len(image.shape) == 2:
          image = gray2rgb(image)
      if random_seed is None:
          random_seed = self.random_state.randint(0, high=1000)
  
      if segmentation_fn is None:
          segmentation_fn = SegmentationAlgorithm(
              "quickshift",
              kernel_size=4,
              max_dist=200,
              ratio=0.2,
              random_seed=random_seed,
          )
      segments = segmentation_fn(image)
  
      fudged_image = image.copy()
      if hide_color is None:
          for x in np.unique(segments):
              fudged_image[segments == x] = (
                  np.mean(image[segments == x][:, 0]),
                  np.mean(image[segments == x][:, 1]),
                  np.mean(image[segments == x][:, 2]),
              )
      else:
          fudged_image[:] = hide_color
  
      top = labels
  
      data, labels = self.data_labels(
          image,
          fudged_image,
          segments,
          classifier_fn,
          num_samples,
          batch_size=batch_size,
          progress_bar=progress_bar,
      )
  
      distances = sklearn.metrics.pairwise_distances(
          data, data[0].reshape(1, -1), metric=distance_metric
      ).ravel()
  
      ret_exp = ImageExplanation(image, segments)
      if top_labels:
          top = np.argsort(labels[0])[-top_labels:]
          ret_exp.top_labels = list(top)
          ret_exp.top_labels.reverse()
      for label in top:
          (
              ret_exp.intercept[label],
              ret_exp.local_exp[label],
              ret_exp.score[label],
              ret_exp.local_pred[label],
          ) = self.base.explain_instance_with_data(
              data,
              labels,
              distances,
              label,
              num_features,
              model_regressor=model_regressor,
              feature_selection=self.feature_selection,
          )
      return ret_exp
  
file_path: lime/lime/lime_image.py
imports: import copy
  from functools import partial
  import numpy
  import sklearn
  from sklearn.utils import check_random_state
  from skimage.color import gray2rgb
  from tqdm.auto import tqdm
  from .None import lime_base
  from .wrappers.scikit_image import SegmentationAlgorithm
label_desc: Use image node_id 2 for calling the LimeImageExplainer obj's underlying member method code with example usage: obj.explain_instance(image, classifier_fn, labels, hide_color, top_labels, num_features, num_samples, batch_size, segmentation_fn, distance_metric, model_regressor, random_seed, progress_bar) and returns: ret_exp