code: def balanced_sampling(y, n_samples=10):
      """
      Performs a class-balanced random sampling.
  
      If `n_samples` is not divisible by the number of classes, a number of samples equal to the
      remainder will be sampled randomly among the classes.
  
      Parameters
      ----------
      y : list of int or numpy.ndarray
          List of labels.
      n_samples : int, default=10
          Number of indices to sample.
  
      Returns
      -------
      indices : numpy.ndarray
          Indices of the stratified subset.
  
      Notes
      -----
      Only useful for experimental simulations (Requires label knowledge).
      """
      _assert_sample_size(y, n_samples)
      if not isinstance(y, np.ndarray):
          y = np.array(y)
  
      # TODO: check for gaps in given labels
  
      # num classes according to the labels
      num_classes = np.max(y) + 1
      # num classes encountered
      num_classes_present = len(np.unique(y))
  
      counts = _get_class_histogram(y, num_classes)
      expected_samples_per_class = np.array(
          [int(n_samples / num_classes_present)] * num_classes
      )
  
      return _random_sampling(
          n_samples, num_classes, expected_samples_per_class, counts, y
      )
  
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 3 for calling a global function with example usage: balanced_sampling(y, n_samples) and returns: _random_sampling