class_docstr: The Minimal Targeted Counterfactual generator for tabular data.
  
  This generator looks for counterfactuals that are close to the original input
  without using gradients. First, the generator finds counterfactual examples
  from the dataset. Then, it changes features in the original input towards the
  counterfactual input until the decision boundary is found. During the search
  for a closer counterfactual, the algorithm manipulates different subsets of
  features, while keeping other features frozen. Thus, the algorithm tries to
  find the closest counterfactual that differs only in a single feature value,
  two feature values, etc., up to the selected "Maximum number of columns to
  change" configuration parameter. Only scalar and categorical features are
  changed during the search. The features of other types are always assigned the
  values of the original input.
  
  The implementation supports both classification and regression models. In case
  of a regression model, the "Regression threshold" parameter sets a decision
  boundary between the 'true' and 'false' values.
  
  The generator weakly guarantees that the result set of counterfactuals
  is minimal, i.e. for every counterfactual in the set, there exist no other
  counterfactual that differs in the same or smaller set of features and is
  closer to the original input.
class_docstr_tok: ["The","Minimal","Targeted","Counterfactual","generator","for","tabular","data",".","This","generator","looks","for","counterfactuals","that","are","close","to","the","original","input","without","using","gradients",".","First",",","the","generator","finds","counterfactual","examples","from","the","dataset",".","Then",",","it","changes","features","in","the","original","input","towards","the","counterfactual","input","until","the","decision","boundary","is","found",".","During","the","search","for","a","closer","counterfactual",",","the","algorithm","manipulates","different","subsets","of","features",",","while","keeping","other","features","frozen",".","Thus",",","the","algorithm","tries","to","find","the","closest","counterfactual","that","differs","only","in","a","single","feature","value",",","two","feature","values",",","etc.",",","up","to","the","selected","``","Maximum","number","of","columns","to","change","''","configuration","parameter",".","Only","scalar","and","categorical","features","are","changed","during","the","search",".","The","features","of","other","types","are","always","assigned","the","values","of","the","original","input",".","The","implementation","supports","both","classification","and","regression","models",".","In","case","of","a","regression","model",",","the","``","Regression","threshold","''","parameter","sets","a","decision","boundary","between","the","'true","'","and","'false","'","values",".","The","generator","weakly","guarantees","that","the","result","set","of","counterfactuals","is","minimal",",","i.e",".","for","every","counterfactual","in","the","set",",","there","exist","no","other","counterfactual","that","differs","in","the","same","or","smaller","set","of","features","and","is","closer","to","the","original","input","."]
docstr: Looks for the decision boundary between two examples using interpolation.
  
  The method searches for a flip that is closer to the `target example` than
  `known_flip`. The method performs the binary search by interpolating scalar
  values.
  
  Args:
    ref_example: an example for which the flip is searched.
    known_flip: an example that represents a known flip.
    target_pred: the model prediction at `ref_example`.
    pred_key: the named of the field inside `target_pred` that holds the
      prediction value.
    model: model to use for running predictions.
    dataset: dataset that contains `known_flip`.
    regression_threshold: threshold to use for regression models.
    max_attempts: number of binary search attempts.
  
  Returns:
    The counterfactual (flip) if found; 'None' otherwise.
docstr_tok: ["Looks","for","the","decision","boundary","between","two","examples","using","interpolation",".","The","method","searches","for","a","flip","that","is","closer","to","the","`","target","example","`","than","`","known_flip","`",".","The","method","performs","the","binary","search","by","interpolating","scalar","values",".","Args",":","ref_example",":","an","example","for","which","the","flip","is","searched",".","known_flip",":","an","example","that","represents","a","known","flip",".","target_pred",":","the","model","prediction","at","`","ref_example","`",".","pred_key",":","the","named","of","the","field","inside","`","target_pred","`","that","holds","the","prediction","value",".","model",":","model","to","use","for","running","predictions",".","dataset",":","dataset","that","contains","`","known_flip","`",".","regression_threshold",":","threshold","to","use","for","regression","models",".","max_attempts",":","number","of","binary","search","attempts",".","Returns",":","The","counterfactual","(","flip",")","if","found",";","'None","'","otherwise","."]
code: def _find_closer_flip_using_interpolation(
      self,
      ref_example: JsonDict,
      known_flip: JsonDict,
      target_pred: JsonDict,
      pred_key: str,
      model: lit_model.Model,
      dataset: lit_dataset.Dataset,
      regression_threshold: Optional[float] = None,
      max_attempts: int = 4,
  ) -> Optional[JsonDict]:
      """Looks for the decision boundary between two examples using interpolation.
  
      The method searches for a flip that is closer to the `target example` than
      `known_flip`. The method performs the binary search by interpolating scalar
      values.
  
      Args:
        ref_example: an example for which the flip is searched.
        known_flip: an example that represents a known flip.
        target_pred: the model prediction at `ref_example`.
        pred_key: the named of the field inside `target_pred` that holds the
          prediction value.
        model: model to use for running predictions.
        dataset: dataset that contains `known_flip`.
        regression_threshold: threshold to use for regression models.
        max_attempts: number of binary search attempts.
  
      Returns:
        The counterfactual (flip) if found; 'None' otherwise.
      """
      min_alpha = 0.0
      max_alpha = 1.0
      closest_flip = None
      input_spec = model.input_spec()
      has_scalar = False
      for _ in range(max_attempts):
          # Interpolate the scalar values using binary search.
          current_alpha = (min_alpha + max_alpha) / 2
          candidate = dict(known_flip)
          for field in ref_example:
              if (
                  field in candidate
                  and field in input_spec
                  and isinstance(input_spec[field], lit_types.Scalar)
                  and candidate[field] is not None
                  and ref_example[field] is not None
              ):
                  candidate[field] = (
                      known_flip[field] * (1 - current_alpha)
                      + ref_example[field] * current_alpha
                  )
                  has_scalar = True
          # The interpolation makes sense only for scalar values. If there are no
          # scalar fields that can be interpolated then terminate the search.
          if not has_scalar:
              return None
          flip, predicted_value = self._is_flip(
              model=model,
              cf_example=candidate,
              orig_output=target_pred,
              pred_key=pred_key,
              regression_thresh=regression_threshold,
          )
          if flip:
              self._find_dataset_parent_and_set(
                  model_output_spec=model.output_spec(),
                  pred_key=pred_key,
                  dataset_spec=dataset.spec(),
                  example=candidate,
                  predicted_value=predicted_value,
              )
              closest_flip = candidate
              min_alpha = current_alpha
          else:
              max_alpha = current_alpha
      return closest_flip
  
imports: import collections
  from typing import Any, cast, Optional
  from absl import logging
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.components import cf_utils
  from lit_nlp.lib import caching
  from lit_nlp.lib import utils
  import numpy
label_desc: Use image node_id 6 for calling the TabularMTC obj's underlying member method code with example usage: obj._find_closer_flip_using_interpolation(ref_example, known_flip, target_pred, pred_key, model, dataset, regression_threshold, max_attempts) and returns: closest_flip, None