class_docstr: Counterfactual Reinforcement Learning Tabular. 
class_docstr_tok: ["Counterfactual","Reinforcement","Learning","Tabular","."]
docstr: Computes counterfactuals for the given instances conditioned on the target and the conditional vector.
  
  Parameters
  ----------
  X
      Input instances to generate counterfactuals for.
  Y_t
      Target labels.
  C
      List of conditional dictionaries. If ``None``, it means that no conditioning was used during training
      (i.e. the `conditional_func` returns ``None``). If conditioning was used during training but no
      conditioning is desired for the current input, an empty list is expected.
  diversity
      Whether to generate diverse counterfactual set for the given instance. Only supported for a single
      input instance.
  num_samples
      Number of diversity samples to be generated. Considered only if ``diversity=True``.
  batch_size
      Batch size to use when generating counterfactuals.
  patience
      Maximum number of iterations to perform diversity search stops. If -1, the search stops only if
      the desired number of samples has been found.
  tolerance
      Tolerance to distinguish two counterfactual instances.
  
  Returns
  -------
  explanation
      `Explanation` object containing the counterfactual with additional metadata as attributes.             See usage `CFRL examples`_ for details.
  
      .. _CFRL examples:
          https://docs.seldon.io/projects/alibi/en/stable/methods/CFRL.html
docstr_tok: ["Computes","counterfactuals","for","the","given","instances","conditioned","on","the","target","and","the","conditional","vector",".","Parameters","--","--","--","--","--","X","Input","instances","to","generate","counterfactuals","for",".","Y_t","Target","labels",".","C","List","of","conditional","dictionaries",".","If","``","None","``",",","it","means","that","no","conditioning","was","used","during","training","(","i.e",".","the","`","conditional_func","`","returns","``","None","``",")",".","If","conditioning","was","used","during","training","but","no","conditioning","is","desired","for","the","current","input",",","an","empty","list","is","expected",".","diversity","Whether","to","generate","diverse","counterfactual","set","for","the","given","instance",".","Only","supported","for","a","single","input","instance",".","num_samples","Number","of","diversity","samples","to","be","generated",".","Considered","only","if","``","diversity=True","``",".","batch_size","Batch","size","to","use","when","generating","counterfactuals",".","patience","Maximum","number","of","iterations","to","perform","diversity","search","stops",".","If","-1",",","the","search","stops","only","if","the","desired","number","of","samples","has","been","found",".","tolerance","Tolerance","to","distinguish","two","counterfactual","instances",".","Returns","--","--","--","-","explanation","`","Explanation","`","object","containing","the","counterfactual","with","additional","metadata","as","attributes",".","See","usage","`","CFRL","examples","`","_","for","details",".","..","_CFRL","examples",":","https",":","\/\/docs.seldon.io\/projects\/alibi\/en\/stable\/methods\/CFRL.html"]
code: def explain(
      self,  # type: ignore[override]
      X: np.ndarray,
      Y_t: np.ndarray,
      C: Optional[List[Dict[str, List[Union[str, float]]]]] = None,
      batch_size: int = 100,
      diversity: bool = False,
      num_samples: int = 1,
      patience: int = 1000,
      tolerance: float = 1e-3,
  ) -> Explanation:
      """
          Computes counterfactuals for the given instances conditioned on the target and the conditional vector.
  
          Parameters
          ----------
          X
              Input instances to generate counterfactuals for.
          Y_t
              Target labels.
          C
              List of conditional dictionaries. If ``None``, it means that no conditioning was used during training
              (i.e. the `conditional_func` returns ``None``). If conditioning was used during training but no
              conditioning is desired for the current input, an empty list is expected.
          diversity
              Whether to generate diverse counterfactual set for the given instance. Only supported for a single
              input instance.
          num_samples
              Number of diversity samples to be generated. Considered only if ``diversity=True``.
          batch_size
              Batch size to use when generating counterfactuals.
          patience
              Maximum number of iterations to perform diversity search stops. If -1, the search stops only if
              the desired number of samples has been found.
          tolerance
              Tolerance to distinguish two counterfactual instances.
  
          Returns
          -------
          explanation
              `Explanation` object containing the counterfactual with additional metadata as attributes. \
              See usage `CFRL examples`_ for details.
  
              .. _CFRL examples:
                  https://docs.seldon.io/projects/alibi/en/stable/methods/CFRL.html
          """
      # General validation.
      self._validate_input(X)
      self._validate_target(Y_t)
  
      # Check if diversity flag is on.
      if diversity:
          return self._diversity(
              X=X,
              Y_t=Y_t,
              C=C,
              num_samples=num_samples,
              batch_size=batch_size,
              patience=patience,
              tolerance=tolerance,
          )
  
      # Get conditioning for a zero input. Used for a sanity check of the user-specified conditioning.
      X_zeros = np.zeros((1, X.shape[1]))
      C_zeros = self.params["conditional_func"](X_zeros)
  
      # If the conditional vector is `None`. This is equivalent of no conditioning at all, not even during training.
      if C is None:
          # Check if the conditional function actually a `None` conditioning
          if C_zeros is not None:
              raise ValueError(
                  "A `None` conditioning is not a valid input when training with conditioning. "
                  "If no feature conditioning is desired for the given input, `C` is expected to be an "
                  "empty list. A `None` conditioning is valid only when no conditioning was used "
                  "during training (i.e. `conditional_func` returns `None`)."
              )
  
          return super().explain(
              X=X, Y_t=Y_t, C=C, batch_size=batch_size
          )
  
      elif C_zeros is None:
          raise ValueError(
              "Conditioning different than `None` is not a valid input when training without "
              "conditioning. If feature conditioning is desired, consider defining an appropriate "
              "`conditional_func` that does not return `None`."
          )
  
      # Define conditional vector if an empty list. This is equivalent of no conditioning, but the conditional
      # vector was used during training.
      if len(C) == 0:
          C = [dict()]
  
      # Check the number of conditions.
      if len(C) != 1 and len(C) != X.shape[0]:
          raise ValueError(
              "The number of conditions should be 1 or equals the number of samples in X."
          )
  
      # If only one condition is passed.
      if len(C) == 1:
          C_vec = self.params["conditional_vector"](
              X=X, condition=C[0], stats=self.params["stats"]
          )
      else:
          # If multiple conditions were passed.
          C_vecs = []
  
          for i in range(len(C)):
              # Generate conditional vector for each instance. Note that this depends on the input instance.
              C_vecs.append(
                  self.params["conditional_vector"](
                      X=np.atleast_2d(X[i]),
                      condition=C[i],
                      stats=self.params["stats"],
                  )
              )
  
          # Concatenate all conditional vectors.
          C_vec = np.concatenate(C_vecs, axis=0)
  
      explanation = super().explain(
          X=X, Y_t=Y_t, C=C_vec, batch_size=batch_size
      )
      explanation.data.update({"condition": C})
      return explanation
  
imports: from functools import partial
  from itertools import count
  from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union, cast
  import numpy
  from tqdm import tqdm
  from alibi.api.interfaces import Explainer, Explanation
  from alibi.explainers.backends.cfrl_tabular import get_conditional_vector, get_statistics, sample
  from alibi.explainers.cfrl_base import _PARAM_TYPES, CounterfactualRL, Postprocessing
  from alibi.utils.frameworks import has_pytorch, has_tensorflow
label_desc: Use image node_id 5 for calling the CounterfactualRLTabular obj's underlying member method code with example usage: obj.explain(X, Y_t, C, batch_size, diversity, num_samples, patience, tolerance) and returns: explanation, self, super