docstr: Explain a prediction instance using the LIME text explainer.
  Like with `build_explainer`, the parameters of `explain_instance` are exactly those of
  lime.lime_text.LimeTextExplainer.explain_instance.
  More documentation can be found at:
  https://lime-ml.readthedocs.io/en/latest/lime.html#lime.lime_text.LimeTextExplainer.explain_instance
  
  Args:
      instance (np.ndarray): A 1D numpy array corresponding to a row/single example
      labels (list): The list of class indexes to produce explanations for
      top_labels (int): If not None, this overwrites labels and the explainer instead produces
          explanations for the top k classes
      num_features (int): Number of features to include in an explanation
      num_samples (int): The number of perturbed samples to train the LIME model with
      distance_metric (str): The distance metric to use for weighting the loss function
  
  Returns:
      (dict) A mapping of class to explanations
  
  Raises:
      ExplainerUninitializedError: Raised if self.explainer_object is None
docstr_tok: ["Explain","a","prediction","instance","using","the","LIME","text","explainer",".","Like","with","`","build_explainer","`",",","the","parameters","of","`","explain_instance","`","are","exactly","those","of","lime.lime_text.LimeTextExplainer.explain_instance",".","More","documentation","can","be","found","at",":","https",":","\/\/lime-ml.readthedocs.io\/en\/latest\/lime.html","#","lime.lime_text.LimeTextExplainer.explain_instance","Args",":","instance","(","np.ndarray",")",":","A","1D","numpy","array","corresponding","to","a","row\/single","example","labels","(","list",")",":","The","list","of","class","indexes","to","produce","explanations","for","top_labels","(","int",")",":","If","not","None",",","this","overwrites","labels","and","the","explainer","instead","produces","explanations","for","the","top","k","classes","num_features","(","int",")",":","Number","of","features","to","include","in","an","explanation","num_samples","(","int",")",":","The","number","of","perturbed","samples","to","train","the","LIME","model","with","distance_metric","(","str",")",":","The","distance","metric","to","use","for","weighting","the","loss","function","Returns",":","(","dict",")","A","mapping","of","class","to","explanations","Raises",":","ExplainerUninitializedError",":","Raised","if","self.explainer_object","is","None"]
code: def explain_instance(
      self,
      instance: np.ndarray,
      labels: List = (1,),
      top_labels: Optional[int] = None,
      num_features: Optional[int] = NUM_TOP_FEATURES,
      num_samples: int = 5000,
      distance_metric: str = "cosine",
      **kwargs,
  ) -> Dict[int, Dict]:
      """
      Explain a prediction instance using the LIME text explainer.
      Like with `build_explainer`, the parameters of `explain_instance` are exactly those of
      lime.lime_text.LimeTextExplainer.explain_instance.
      More documentation can be found at:
      https://lime-ml.readthedocs.io/en/latest/lime.html#lime.lime_text.LimeTextExplainer.explain_instance
  
      Args:
          instance (np.ndarray): A 1D numpy array corresponding to a row/single example
          labels (list): The list of class indexes to produce explanations for
          top_labels (int): If not None, this overwrites labels and the explainer instead produces
              explanations for the top k classes
          num_features (int): Number of features to include in an explanation
          num_samples (int): The number of perturbed samples to train the LIME model with
          distance_metric (str): The distance metric to use for weighting the loss function
  
      Returns:
          (dict) A mapping of class to explanations
  
      Raises:
          ExplainerUninitializedError: Raised if self.explainer_object is None
      """
      if self.explainer_object:
          explanation = self.explainer_object.explain_instance(
              text_instance=instance,
              classifier_fn=self.predict_fn,
              labels=labels,
              top_labels=top_labels,
              num_features=num_features,
              num_samples=num_samples,
              distance_metric=distance_metric,
          )
  
          if top_labels:
              labels_to_extract = list(explanation.as_map().keys())
          else:
              labels_to_extract = labels
  
          confidences = explanation.predict_proba
  
          return explanation_to_json(
              explanation,
              labels_to_extract,
              confidences,
              mode=MODE.CLASSIFICATION,
          )
      else:
          raise ExplainerUninitializedError(
              "This explainer is not yet instantiated! "
              "Please call build_explainer()"
              "first before calling explain_instance."
          )
  
file_path: contextual-ai/xai/explainer/text/lime_text_explainer.py
imports: import warnings
  from typing import List, Optional, Callable, Dict
  import dill
  import numpy
  from lime.lime_text import LimeTextExplainer
  from ..abstract_explainer import AbstractExplainer
  from ..explainer_exceptions import ExplainerUninitializedError
  from ..utils import explanation_to_json
  from ..constants import MODE
label_desc: Use image node_id 3 for calling the LimeTextExplainer obj's underlying member method code with example usage: obj.explain_instance(instance, labels, top_labels, num_features, num_samples, distance_metric) and returns: explanation_to_json