class_docstr: Counterfactual Reinforcement Learning Tabular. 
class_docstr_tok: ["Counterfactual","Reinforcement","Learning","Tabular","."]
docstr: Constructor.
  
  Parameters
  ----------
  predictor
      A callable that takes a `numpy` array of `N` data points as inputs and returns `N` outputs. For
      classification task, the second dimension of the output should match the number of classes. Thus, the
      output can be either a soft label distribution or a hard label distribution (i.e. one-hot encoding)
      without affecting the performance since `argmax` is applied to the predictor's output.
  encoder
      Pretrained heterogeneous encoder network.
  decoder
      Pretrained heterogeneous decoder network. The output of the decoder must be a list of tensors.
  encoder_preprocessor
      Auto-encoder data pre-processor. Depending on the input format, the pre-processor can normalize
      numerical attributes, transform label encoding to one-hot encoding etc.
  decoder_inv_preprocessor
      Auto-encoder data inverse pre-processor. This is the inverse function of the pre-processor. It can
      denormalize numerical attributes, transform one-hot encoding to label encoding, feature type casting etc.
  coeff_sparsity
     Sparsity loss coefficient.
  coeff_consistency
     Consistency loss coefficient.
  feature_names
      List of feature names. This should be provided by the dataset.
  category_map
      Dictionary of category mapping. The keys are column indexes and the values are lists containing the possible
      values for a feature. This should be provided by the dataset.
  immutable_features
      List of immutable features.
  ranges
      Numerical feature ranges. Note that exist numerical features such as ``'Age'``, which are  allowed to
      increase only. We denote those by ``'inc_feat'``. Similarly, there exist features  allowed to decrease only.
      We denote them by ``'dec_feat'``. Finally, there are some free feature, which we denote by ``'free_feat'``.
      With the previous notation, we can define ``range = {'inc_feat': [0, 1], 'dec_feat': [-1, 0],
      'free_feat': [-1, 1]}``. ``'free_feat'`` can be omitted, as any unspecified feature is considered free.
      Having the ranges of a feature `{'feat': [a_low, a_high}`, when sampling is performed the numerical value
      will be clipped between `[a_low * (max_val - min_val), a_high * [max_val - min_val]]`, where `a_low` and
      `a_high` are the minimum and maximum values the feature ``'feat'``. This implies that `a_low` and `a_high`
      are not restricted to ``{-1, 0}`` and ``{0, 1}``, but can be any float number in-between `[-1, 0]` and
      `[0, 1]`.
  weight_num
      Numerical loss weight.
  weight_cat
      Categorical loss weight.
  latent_dim
      Auto-encoder latent dimension. Can be omitted if the actor network is user specified.
  backend
     Deep learning backend: ``'tensorflow'`` | ``'pytorch'``. Default ``'tensorflow'``.
  seed
      Seed for reproducibility. The results are not reproducible for ``'tensorflow'`` backend.
  **kwargs
      Used to replace any default parameter from :py:data:`alibi.explainers.cfrl_base.DEFAULT_BASE_PARAMS`.
docstr_tok: ["Constructor",".","Parameters","--","--","--","--","--","predictor","A","callable","that","takes","a","`","numpy","`","array","of","`","N","`","data","points","as","inputs","and","returns","`","N","`","outputs",".","For","classification","task",",","the","second","dimension","of","the","output","should","match","the","number","of","classes",".","Thus",",","the","output","can","be","either","a","soft","label","distribution","or","a","hard","label","distribution","(","i.e",".","one-hot","encoding",")","without","affecting","the","performance","since","`","argmax","`","is","applied","to","the","predictor","'s","output",".","encoder","Pretrained","heterogeneous","encoder","network",".","decoder","Pretrained","heterogeneous","decoder","network",".","The","output","of","the","decoder","must","be","a","list","of","tensors",".","encoder_preprocessor","Auto-encoder","data","pre-processor",".","Depending","on","the","input","format",",","the","pre-processor","can","normalize","numerical","attributes",",","transform","label","encoding","to","one-hot","encoding","etc",".","decoder_inv_preprocessor","Auto-encoder","data","inverse","pre-processor",".","This","is","the","inverse","function","of","the","pre-processor",".","It","can","denormalize","numerical","attributes",",","transform","one-hot","encoding","to","label","encoding",",","feature","type","casting","etc",".","coeff_sparsity","Sparsity","loss","coefficient",".","coeff_consistency","Consistency","loss","coefficient",".","feature_names","List","of","feature","names",".","This","should","be","provided","by","the","dataset",".","category_map","Dictionary","of","category","mapping",".","The","keys","are","column","indexes","and","the","values","are","lists","containing","the","possible","values","for","a","feature",".","This","should","be","provided","by","the","dataset",".","immutable_features","List","of","immutable","features",".","ranges","Numerical","feature","ranges",".","Note","that","exist","numerical","features","such","as","``","'Age","'","``",",","which","are","allowed","to","increase","only",".","We","denote","those","by","``","'inc_feat","'","``",".","Similarly",",","there","exist","features","allowed","to","decrease","only",".","We","denote","them","by","``","'dec_feat","'","``",".","Finally",",","there","are","some","free","feature",",","which","we","denote","by","``","'free_feat","'","``",".","With","the","previous","notation",",","we","can","define","``","range","=","{","'inc_feat","'",":","[","0",",","1","]",",","'dec_feat","'",":","[","-1",",","0","]",",","'free_feat","'",":","[","-1",",","1","]","}","``",".","``","'free_feat","'","``","can","be","omitted",",","as","any","unspecified","feature","is","considered","free",".","Having","the","ranges","of","a","feature","`","{","'feat","'",":","[","a_low",",","a_high","}","`",",","when","sampling","is","performed","the","numerical","value","will","be","clipped","between","`","[","a_low","*","(","max_val","-","min_val",")",",","a_high","*","[","max_val","-","min_val","]","]","`",",","where","`","a_low","`","and","`","a_high","`","are","the","minimum","and","maximum","values","the","feature","``","'feat","'","``",".","This","implies","that","`","a_low","`","and","`","a_high","`","are","not","restricted","to","``","{","-1",",","0","}","``","and","``","{","0",",","1","}","``",",","but","can","be","any","float","number","in-between","`","[","-1",",","0","]","`","and","`","[","0",",","1","]","`",".","weight_num","Numerical","loss","weight",".","weight_cat","Categorical","loss","weight",".","latent_dim","Auto-encoder","latent","dimension",".","Can","be","omitted","if","the","actor","network","is","user","specified",".","backend","Deep","learning","backend",":","``","'tensorflow","'","``","|","``","'pytorch","'","``",".","Default","``","'tensorflow","'","``",".","seed","Seed","for","reproducibility",".","The","results","are","not","reproducible","for","``","'tensorflow","'","``","backend",".","*","*","kwargs","Used","to","replace","any","default","parameter","from",":","py",":","data",":","`","alibi.explainers.cfrl_base.DEFAULT_BASE_PARAMS","`","."]
code: def __init__(
      self,
      predictor: Callable[[np.ndarray], np.ndarray],
      encoder: "Union[tensorflow.keras.Model, torch.nn.Module]",
      decoder: "Union[tensorflow.keras.Model, torch.nn.Module]",
      encoder_preprocessor: Callable,
      decoder_inv_preprocessor: Callable,
      coeff_sparsity: float,
      coeff_consistency: float,
      feature_names: List[str],
      category_map: Dict[int, List[str]],
      immutable_features: Optional[List[str]] = None,
      ranges: Optional[Dict[str, Tuple[int, int]]] = None,
      weight_num: float = 1.0,
      weight_cat: float = 1.0,
      latent_dim: Optional[int] = None,
      backend: str = "tensorflow",
      seed: int = 0,
      **kwargs,
  ):
      """
      Constructor.
  
      Parameters
      ----------
      predictor
          A callable that takes a `numpy` array of `N` data points as inputs and returns `N` outputs. For
          classification task, the second dimension of the output should match the number of classes. Thus, the
          output can be either a soft label distribution or a hard label distribution (i.e. one-hot encoding)
          without affecting the performance since `argmax` is applied to the predictor's output.
      encoder
          Pretrained heterogeneous encoder network.
      decoder
          Pretrained heterogeneous decoder network. The output of the decoder must be a list of tensors.
      encoder_preprocessor
          Auto-encoder data pre-processor. Depending on the input format, the pre-processor can normalize
          numerical attributes, transform label encoding to one-hot encoding etc.
      decoder_inv_preprocessor
          Auto-encoder data inverse pre-processor. This is the inverse function of the pre-processor. It can
          denormalize numerical attributes, transform one-hot encoding to label encoding, feature type casting etc.
      coeff_sparsity
         Sparsity loss coefficient.
      coeff_consistency
         Consistency loss coefficient.
      feature_names
          List of feature names. This should be provided by the dataset.
      category_map
          Dictionary of category mapping. The keys are column indexes and the values are lists containing the possible
          values for a feature. This should be provided by the dataset.
      immutable_features
          List of immutable features.
      ranges
          Numerical feature ranges. Note that exist numerical features such as ``'Age'``, which are  allowed to
          increase only. We denote those by ``'inc_feat'``. Similarly, there exist features  allowed to decrease only.
          We denote them by ``'dec_feat'``. Finally, there are some free feature, which we denote by ``'free_feat'``.
          With the previous notation, we can define ``range = {'inc_feat': [0, 1], 'dec_feat': [-1, 0],
          'free_feat': [-1, 1]}``. ``'free_feat'`` can be omitted, as any unspecified feature is considered free.
          Having the ranges of a feature `{'feat': [a_low, a_high}`, when sampling is performed the numerical value
          will be clipped between `[a_low * (max_val - min_val), a_high * [max_val - min_val]]`, where `a_low` and
          `a_high` are the minimum and maximum values the feature ``'feat'``. This implies that `a_low` and `a_high`
          are not restricted to ``{-1, 0}`` and ``{0, 1}``, but can be any float number in-between `[-1, 0]` and
          `[0, 1]`.
      weight_num
          Numerical loss weight.
      weight_cat
          Categorical loss weight.
      latent_dim
          Auto-encoder latent dimension. Can be omitted if the actor network is user specified.
      backend
         Deep learning backend: ``'tensorflow'`` | ``'pytorch'``. Default ``'tensorflow'``.
      seed
          Seed for reproducibility. The results are not reproducible for ``'tensorflow'`` backend.
      **kwargs
          Used to replace any default parameter from :py:data:`alibi.explainers.cfrl_base.DEFAULT_BASE_PARAMS`.
      """
      super().__init__(
          encoder=encoder,
          decoder=decoder,
          latent_dim=latent_dim,
          predictor=predictor,
          coeff_sparsity=coeff_sparsity,
          coeff_consistency=coeff_consistency,
          backend=backend,
          seed=seed,
          **kwargs,
      )
  
      # Set encoder preprocessor and decoder inverse preprocessor.
      self.params["encoder_preprocessor"] = encoder_preprocessor
      self.params["decoder_inv_preprocessor"] = decoder_inv_preprocessor
  
      # Set dataset specific arguments.
      self.params["category_map"] = category_map
      self.params["feature_names"] = feature_names
      self.params["ranges"] = ranges if (ranges is not None) else dict()
      self.params["immutable_features"] = (
          immutable_features
          if (immutable_features is not None)
          else list()
      )
      self.params["weight_num"] = weight_num
      self.params["weight_cat"] = weight_cat
  
      # Set sparsity loss if not user-specified.
      if "sparsity_loss" not in kwargs:
          self.params["sparsity_loss"] = partial(
              self.backend.sparsity_loss,
              category_map=self.params["category_map"],
              weight_num=weight_num,
              weight_cat=weight_cat,
          )
  
      # Set consistency loss if not user-specified.
      if "consistency_loss" not in kwargs:
          self.params[
              "consistency_loss"
          ] = self.backend.consistency_loss
  
      # Set training conditional function generator if not user-specified.
      if "conditional_func" not in kwargs:
          self.params["conditional_func"] = partial(
              self.backend.generate_condition,
              feature_names=self.params["feature_names"],
              category_map=self.params["category_map"],
              ranges=self.params["ranges"],
              immutable_features=self.params["immutable_features"],
          )
  
      # Set testing conditional function generator if not user-specified.
      if "conditional_vector" not in kwargs:
          self.params["conditional_vector"] = partial(
              get_conditional_vector,
              preprocessor=self.params["encoder_preprocessor"],
              feature_names=self.params["feature_names"],
              category_map=self.params["category_map"],
              ranges=self.params["ranges"],
              immutable_features=self.params["immutable_features"],
          )
  
      # update metadata
      self.meta["params"].update(
          CounterfactualRLTabular._serialize_params(self.params)
      )
  
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 1 to create a new CounterfactualRLTabular object from inherited base classes: CounterfactualRL with example: obj = CounterfactualRLTabular(predictor, encoder, decoder, encoder_preprocessor, decoder_inv_preprocessor, coeff_sparsity, coeff_consistency, feature_names, category_map, immutable_features, ranges, weight_num, weight_cat, latent_dim, backend, seed)