code: def calibrate_confident_joint(
      confident_joint: np.ndarray,
      labels: LabelLike,
      *,
      multi_label: bool = False,
  ) -> np.ndarray:
      """Calibrates any confident joint estimate ``P(label=i, true_label=j)`` such that
      ``np.sum(cj) == len(labels)`` and ``np.sum(cj, axis = 1) == np.bincount(labels)``.
  
      In other words, this function forces the confident joint to have the
      true noisy prior ``p(labels)`` (summed over columns for each row) and also
      forces the confident joint to add up to the total number of examples.
  
      This method makes the confident joint a valid counts estimate
      of the actual joint of noisy and true labels.
  
      Parameters
      ----------
      confident_joint : np.ndarray
        An array of shape ``(K, K)`` representing the confident joint, the matrix used for identifying label issues, which
        estimates a confident subset of the joint distribution of the noisy and true labels, ``P_{noisy label, true label}``.
        Entry ``(j, k)`` in the matrix is the number of examples confidently counted into the pair of ``(noisy label=j, true label=k)`` classes.
        The `confident_joint` can be computed using `~cleanlab.count.compute_confident_joint`.
        If not provided, it is computed from the given (noisy) `labels` and `pred_probs`.
        If `multi_label` is True, then the `confident_joint` should be a one-vs-rest array of shape ``(K, 2, 2)``, and an array of the same shape will be returned.
  
      labels : np.ndarray or list
        Given class labels for each example in the dataset, some of which may be erroneous,
        in same format expected by :py:func:`filter.find_label_issues <cleanlab.filter.find_label_issues>` function.
  
      multi_label : bool, optional
        If ``False``, dataset is for regular (multi-class) classification, where each example belongs to exactly one class.
        If ``True``, dataset is for multi-label classification, where each example can belong to multiple classes.
        See documentation of `~cleanlab.count.compute_confident_joint` for details.
        In multi-label classification, the confident/calibrated joint arrays have shape ``(K, 2, 2)``
        formatted in a one-vs-rest fashion such that they contain a 2x2 matrix for each class
        that counts examples which are correctly/incorrectly labeled as belonging to that class.
        After calibration, the entries in each class-specific 2x2 matrix will sum to the number of examples.
  
      Returns
      -------
      calibrated_cj : np.ndarray
        An array of shape ``(K, K)`` representing a valid estimate of the joint *counts* of noisy and true labels (if `multi_label` is False).
        If `multi_label` is True, the returned `calibrated_cj` is instead an one-vs-rest array of shape ``(K, 2, 2)``,
        where for class `c`: entry ``(c, 0, 0)`` in this one-vs-rest  array is the number of examples whose noisy label contains `c` confidently identified as truly belonging to class `c` as well.
        Entry ``(c, 1, 0)`` in this one-vs-rest  array is the number of examples whose noisy label contains `c` confidently identified as not actually belonging to class `c`.
        Entry ``(c, 0, 1)`` in this one-vs-rest array is the number of examples whose noisy label does not contain `c` confidently identified as truly belonging to class `c`.
        Entry ``(c, 1, 1)`` in this one-vs-rest array is the number of examples whose noisy label does not contain `c` confidently identified as actually not belonging to class `c` as well.
  
      """
  
      if multi_label:
          if not isinstance(labels, list):
              raise TypeError(
                  "`labels` must be list when `multi_label=True`."
              )
          else:
              return _calibrate_confident_joint_multilabel(
                  confident_joint, labels
              )
      else:
          num_classes = len(confident_joint)
          label_counts = value_counts_fill_missing_classes(
              labels, num_classes, multi_label=False
          )
      # Calibrate confident joint to have correct p(labels) prior on noisy labels.
      calibrated_cj = (
          confident_joint.T
          / np.clip(
              confident_joint.sum(axis=1), a_min=TINY_VALUE, a_max=None
          )
          * label_counts
      ).T
      # Calibrate confident joint to sum to:
      # The number of examples (for single labeled datasets)
      # The number of total labels (for multi-labeled datasets)
      calibrated_cj = (
          calibrated_cj
          / np.clip(np.sum(calibrated_cj), a_min=TINY_VALUE, a_max=None)
          * sum(label_counts)
      )
      return round_preserving_row_totals(calibrated_cj)
  
imports: from sklearn.linear_model import LogisticRegression
  from sklearn.model_selection import StratifiedKFold
  from sklearn.metrics import confusion_matrix
  import sklearn.base
  import numpy
  import warnings
  from typing import Tuple, Union, Optional
  from cleanlab.typing import LabelLike
  from cleanlab.internal.multilabel_utils import stack_complement, get_onehot_num_classes
  from cleanlab.internal.constants import TINY_VALUE, CONFIDENT_THRESHOLDS_LOWER_BOUND, FLOATING_POINT_COMPARISON
  from cleanlab.internal.util import value_counts_fill_missing_classes, clip_values, clip_noise_rates, round_preserving_row_totals, append_extra_datapoint, train_val_split, get_num_classes, get_unique_classes, is_torch_dataset, is_tensorflow_dataset
  from cleanlab.internal.latent_algebra import compute_inv_noise_matrix, compute_py, compute_noise_matrix_from_inverse
  from cleanlab.internal.validation import assert_valid_inputs, labels_to_array
label_desc: Use image node_id 4 for calling a global function with example usage: calibrate_confident_joint(confident_joint, labels) and returns: round_preserving_row_totals, _calibrate_confident_joint_multilabel