docstr: Tests explaining text classifiers with various output dimensions.
docstr_tok: ["Tests","explaining","text","classifiers","with","various","output","dimensions","."]
code: def test_explain(
      self,
      sentence,
      num_samples,
      positive_token,
      negative_token,
      num_classes,
      class_to_explain,
  ):
      """Tests explaining text classifiers with various output dimensions."""
  
      def _predict_fn(sentences):
          """Mock prediction function."""
          rs = np.random.RandomState(seed=0)
          predictions = []
          for sentence in sentences:
              probs = rs.uniform(0.0, 1.0, num_classes)
              # To check if LIME finds the right positive/negative correlations.
              if negative_token in sentence:
                  probs[class_to_explain] = (
                      probs[class_to_explain] - 1.0
                  )
              if positive_token in sentence:
                  probs[class_to_explain] = (
                      probs[class_to_explain] + 1.0
                  )
              predictions.append(probs)
  
          predictions = np.stack(predictions, axis=0)
          if num_classes == 1:
              return np.squeeze(special.expit(predictions), -1)
          else:
              return special.softmax(predictions, axis=-1)
  
      explanation = lime.explain(
          sentence,
          _predict_fn,
          class_to_explain=class_to_explain,
          num_samples=num_samples,
          tokenizer=str.split,
      )
  
      self.assertLen(
          explanation.feature_importance, len(sentence.split())
      )
  
      # The positive word should have the highest attribution score.
      positive_token_idx = sentence.split().index(positive_token)
      self.assertEqual(
          positive_token_idx, np.argmax(explanation.feature_importance)
      )
  
      # The negative word should have the lowest attribution score.
      negative_token_idx = sentence.split().index(negative_token)
      self.assertEqual(
          negative_token_idx, np.argmin(explanation.feature_importance)
      )
  
imports: import collections
  import functools
  from absl.testing import absltest
  from absl.testing import parameterized
  from lime import lime_text
  from lit_nlp.components.citrus import lime
  from lit_nlp.components.citrus import utils
  import numpy
  from scipy import special
  from scipy import stats
label_desc: Use image node_id 6 for calling the LimeTest obj's underlying member method code with example usage: obj.test_explain(sentence, num_samples, positive_token, negative_token, num_classes, class_to_explain) and returns: np, special