code: def _precision_recall(
      predictions, targets, k
  ):  # pylint: disable=invalid-name
      """Returns precision, recall, and intermediate values for the category `k`.
  
      A support function for calculating precision, recall,
      and intermediate values for the single category `k`
      for future use in metric layers.
  
      Args:
        predictions: predicted categories.
        targets: target categories.
        k: a category number.
  
      Returns a tuple:
        n_correct: a number of correct (or true) examples.
        n_k_predictions: a number of predictions of the `k` category.
        n_k_targets: a number of targets for the `k` category.
        precision: a precision score.
        recall: a recall score.
      """
      n_correct = sum((predictions == k) & (targets == k))
      n_k_predictions = sum(predictions == k)
      precision = _non_nan(n_correct / n_k_predictions)
      n_k_targets = sum(targets == k)
      recall = _non_nan(n_correct / n_k_targets)
      return (
          n_correct,
          n_k_predictions,
          n_k_targets,
          precision,
          recall,
      )
  
imports: from trax import fastmath
  from trax import shapes
  from trax.fastmath import numpy
  from trax.layers import base
  from trax.layers import combinators
  from trax.layers import core
label_desc: Use image node_id 4 for calling a global function with example usage: _precision_recall(predictions, targets, k) and returns: n_correct,  n_k_predictions,  n_k_targets,  precision,  recall