code: def compute_inv_noise_matrix(
      py, noise_matrix, *, ps=None
  ) -> np.ndarray:
      """Compute the inverse noise matrix if py := P(true_label=k) is given.
  
      Parameters
      ----------
      py : np.ndarray (shape (K, 1))
          The fraction (prior probability) of each TRUE class label, P(true_label = k)
  
      noise_matrix : np.ndarray
          A conditional probability matrix (of shape ``(K, K)``) of the form ``P(label=k_s|true_label=k_y)`` containing
          the fraction of examples in every class, labeled as every other class.
          Assumes columns of noise_matrix sum to 1.
  
      ps : np.ndarray
          Array of shape ``(K, 1)`` containing the fraction (prior probability) of each NOISY given label, ``P(labels = k)``.
          `ps` is easily computable from py and should only be provided if it has already been precomputed, to increase code efficiency.
  
      Examples
      --------
      For loop based implementation:
  
      .. code:: python
  
          # Number of classes
          K = len(py)
  
          # 'ps' is p(labels=k) = noise_matrix * p(true_labels=k)
          # because in *vector computation*: P(label=k|true_label=k) * p(true_label=k) = P(label=k)
          if ps is None:
              ps = noise_matrix.dot(py)
  
          # Estimate the (K, K) inverse noise matrix P(true_label = k_y | label = k_s)
          inverse_noise_matrix = np.empty(shape=(K,K))
          # k_s is the class value k of noisy label `label == k`
          for k_s in range(K):
              # k_y is the (guessed) class value k of true label y
              for k_y in range(K):
                  # P(true_label|label) = P(label|y) * P(true_label) / P(labels)
                  inverse_noise_matrix[k_y][k_s] = noise_matrix[k_s][k_y] * \
                                                   py[k_y] / ps[k_s]
      """
  
      joint = noise_matrix * py
      ps = joint.sum(axis=1) if ps is None else ps
      inverse_noise_matrix = joint.T / np.clip(
          ps, a_min=TINY_VALUE, a_max=None
      )
  
      # Clip inverse noise rates P(true_label=k_s|true_label=k_y) into proper range [0,1)
      return clip_noise_rates(inverse_noise_matrix)
  
file_path: cleanlab/cleanlab/internal/latent_algebra.py
imports: import warnings
  import numpy
  from typing import Tuple
  from cleanlab.internal.util import value_counts, clip_values, clip_noise_rates
  from cleanlab.internal.constants import TINY_VALUE, CLIPPING_LOWER_BOUND
label_desc: Use image node_id 3 for calling a global function with example usage: compute_inv_noise_matrix(py, noise_matrix) and returns: clip_noise_rates