code: def estimate_confident_joint_and_cv_pred_proba(
      X,
      labels,
      clf=LogReg(multi_class="auto", solver="lbfgs"),
      *,
      cv_n_folds=5,
      thresholds=None,
      seed=None,
      calibrate=True,
      clf_kwargs={},
      validation_func=None,
  ) -> Tuple[np.ndarray, np.ndarray]:
      """Estimates ``P(labels, y)``, the confident counts of the latent
      joint distribution of true and noisy labels
      using observed `labels` and predicted probabilities `pred_probs`.
  
      The output of this function is an array of shape ``(K, K)``.
  
      Under certain conditions, estimates are exact, and in many
      conditions, estimates are within one percent of actual.
  
      Notes: There are two ways to compute the confident joint with pros/cons.
      (1) For each holdout set, we compute the confident joint, then sum them up.
      (2) Compute pred_proba for each fold, combine, compute the confident joint.
      (1) is more accurate because it correctly computes thresholds for each fold
      (2) is more accurate when you have only a little data because it computes
      the confident joint using all the probabilities. For example if you had 100
      examples, with 5-fold cross validation + uniform p(y) you would only have 20
      examples to compute each confident joint for (1). Such small amounts of data
      is bound to result in estimation errors. For this reason, we implement (2),
      but we implement (1) as a commented out function at the end of this file.
  
      Parameters
      ----------
      X : np.ndarray or pd.DataFrame
        Input feature matrix of shape ``(N, ...)``, where N is the number of
        examples. The classifier that this instance was initialized with,
            ``clf``, must be able to fit() and predict() data with this format.
  
      labels : np.ndarray or pd.Series
        A 1D array of shape ``(N,)`` containing class labels for a standard (multi-class) classification dataset.
        Some given labels may be erroneous.
        Elements must be integers in the set 0, 1, ..., K-1, where K is the number of classes.
        All classes must be present in the dataset.
  
      clf : estimator instance, optional
        A classifier implementing the `sklearn estimator API
        <https://scikit-learn.org/stable/developers/develop.html#rolling-your-own-estimator>`_.
  
      cv_n_folds : int, default=5
        The number of cross-validation folds used to compute
        out-of-sample predicted probabilities for each example in `X`.
  
      thresholds : array_like, optional
        An array of shape ``(K, 1)`` or ``(K,)`` of per-class threshold
        probabilities, used to determine the cutoff probability necessary to
        consider an example as a given class label (see `Northcutt et al.,
        2021 <https://jair.org/index.php/jair/article/view/12125>`_, Section
        3.1, Equation 2).
  
        This is for advanced users only. If not specified, these are computed
        for you automatically. If an example has a predicted probability
        greater than this threshold, it is counted as having true_label =
        k. This is not used for pruning/filtering, only for estimating the
        noise rates using confident counts.
  
      seed : int, optional
          Set the default state of the random number generator used to split
          the cross-validated folds. If None, uses np.random current random state.
  
      calibrate : bool, default=True
          Calibrates 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)``.
  
      clf_kwargs : dict, optional
        Optional keyword arguments to pass into `clf`'s ``fit()`` method.
  
      validation_func : callable, optional
        Specifies how to map the validation data split in cross-validation as input for ``clf.fit()``.
        For details, see the documentation of :py:meth:`CleanLearning.fit<cleanlab.classification.CleanLearning.fit>`
  
      Returns
      ------
      estimates : tuple
        Tuple of two numpy arrays in the form:
        (joint counts matrix, predicted probability matrix)
  
      Note
      ----
      Multi-label classification is not supported in this method.
      """
  
      assert_valid_inputs(X, labels)
      labels = labels_to_array(labels)
      num_classes = get_num_classes(
          labels=labels
      )  # This method definitely only works if all classes are present.
  
      # Create cross-validation object for out-of-sample predicted probabilities.
      # CV folds preserve the fraction of noisy positive and
      # noisy negative examples in each class.
      kf = StratifiedKFold(
          n_splits=cv_n_folds, shuffle=True, random_state=seed
      )
  
      # Initialize pred_probs array
      pred_probs = np.zeros(shape=(len(labels), num_classes))
  
      # Split X and labels into "cv_n_folds" stratified folds.
      # CV indices only require labels: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedKFold.html
      # Only split based on labels because X may have various formats:
      for k, (cv_train_idx, cv_holdout_idx) in enumerate(
          kf.split(X=labels, y=labels)
      ):
          try:
              clf_copy = sklearn.base.clone(
                  clf
              )  # fresh untrained copy of the model
          except Exception:
              raise ValueError(
                  "`clf` must be clonable via: sklearn.base.clone(clf). "
                  "You can either implement instance method `clf.get_params()` to produce a fresh untrained copy of this model, "
                  "or you can implement the cross-validation outside of cleanlab "
                  "and pass in the obtained `pred_probs` to skip cleanlab's internal cross-validation"
              )
          # Select the training and holdout cross-validated sets.
          (
              X_train_cv,
              X_holdout_cv,
              s_train_cv,
              s_holdout_cv,
          ) = train_val_split(X, labels, cv_train_idx, cv_holdout_idx)
  
          # dict with keys: which classes missing, values: index of holdout data from this class that is duplicated:
          missing_class_inds = {}
          is_tf_or_torch_dataset = is_torch_dataset(
              X
          ) or is_tensorflow_dataset(X)
          if not is_tf_or_torch_dataset:
              # Ensure no missing classes in training set.
              train_cv_classes = set(s_train_cv)
              all_classes = set(range(num_classes))
              if len(train_cv_classes) != len(all_classes):
                  missing_classes = all_classes.difference(
                      train_cv_classes
                  )
                  warnings.warn(
                      "Duplicated some data across multiple folds to ensure training does not fail "
                      f"because these classes do not have enough data for proper cross-validation: {missing_classes}."
                  )
                  for missing_class in missing_classes:
                      # Duplicate one instance of missing_class from holdout data to the training data:
                      holdout_inds = np.where(
                          s_holdout_cv == missing_class
                      )[0]
                      dup_idx = holdout_inds[0]
                      s_train_cv = np.append(
                          s_train_cv, s_holdout_cv[dup_idx]
                      )
                      # labels are always np.ndarray so don't have to consider .iloc above
                      X_train_cv = append_extra_datapoint(
                          to_data=X_train_cv,
                          from_data=X_holdout_cv,
                          index=dup_idx,
                      )
                      missing_class_inds[missing_class] = dup_idx
  
          # Map validation data into appropriate format to pass into classifier clf
          if validation_func is None:
              validation_kwargs = {}
          elif callable(validation_func):
              validation_kwargs = validation_func(
                  X_holdout_cv, s_holdout_cv
              )
          else:
              raise TypeError(
                  "validation_func must be callable function with args: X_val, y_val"
              )
  
          # Fit classifier clf to training set, predict on holdout set, and update pred_probs.
          clf_copy.fit(
              X_train_cv, s_train_cv, **clf_kwargs, **validation_kwargs
          )
          pred_probs_cv = clf_copy.predict_proba(
              X_holdout_cv
          )  # P(labels = k|x) # [:,1]
  
          # Replace predictions for duplicated indices with dummy predictions:
          for missing_class in missing_class_inds:
              dummy_pred = np.zeros(pred_probs_cv[0].shape)
              dummy_pred[
                  missing_class
              ] = 1.0  # predict given label with full confidence
              dup_idx = missing_class_inds[missing_class]
              pred_probs_cv[dup_idx] = dummy_pred
  
          pred_probs[cv_holdout_idx] = pred_probs_cv
  
      # Compute the confident counts, a num_classes x num_classes matrix for all pairs of labels.
      confident_joint = compute_confident_joint(
          labels=labels,
          pred_probs=pred_probs,  # P(labels = k|x)
          thresholds=thresholds,
          calibrate=calibrate,
      )
      assert isinstance(confident_joint, np.ndarray)
      assert isinstance(pred_probs, np.ndarray)
  
      return confident_joint, pred_probs
  
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 12 for calling a global function with example usage: estimate_confident_joint_and_cv_pred_proba(X, labels, clf) and returns: confident_joint,  pred_probs