code: def _random_sampling(
      n_samples, num_classes, expected_samples_per_class, counts, y
  ):
      remainder = n_samples - expected_samples_per_class.sum()
  
      for i in range(num_classes):
          diff = expected_samples_per_class[i] - counts[i]
          if diff > 0:
              expected_samples_per_class[i] = counts[i]
              remainder += diff
  
      if remainder != 0:
          classes = np.array([i for i in range(num_classes)])
  
          counts_remaining = counts - expected_samples_per_class
  
          for i in range(remainder):
              p_remaining = (
                  counts_remaining.astype(float)
                  / counts_remaining.sum()
              )
  
              class_index = (
                  np.random.choice(
                      classes, size=1, replace=False, p=p_remaining
                  )
                  .flatten()
                  .tolist()[0]
              )
              expected_samples_per_class[class_index] += 1
              counts_remaining[class_index] -= 1
  
      indices = []
      for i in range(num_classes):
          if expected_samples_per_class[i] > 0:
              class_indices = np.argwhere(y == i)[:, 0]
              indices += (
                  np.random.choice(
                      class_indices,
                      size=expected_samples_per_class[i],
                      replace=False,
                  )
                  .flatten()
                  .tolist()
              )
  
      indices = np.random.permutation(indices)
  
      return indices
  
file_path: small-text/small_text/data/sampling.py
imports: import numpy
  from scipy.sparse import csr_matrix
  from sklearn.preprocessing import LabelEncoder
label_desc: Use image node_id 5 for calling a global function with example usage: _random_sampling(n_samples, num_classes, expected_samples_per_class, counts, y) and returns: indices