def filter_factors(labels, num_observed_factors, random_state):
  """Filter observed factor keeping only a random subset of them.

  Args:
    labels: Factors of variations. Numpy array of shape
      (num_labelled_samples, num_factors) of Float32.
    num_observed_factors: How many factors should be kept.
    random_state: Random state used to sample the permutation.

  Returns:
    Filters the labels so that only num_observed_factors are observed.
  """
  if num_observed_factors < 1:
    raise ValueError("Cannot observe negative amount of factors.")
  elif num_observed_factors > labels.shape[1]:
    raise ValueError(
        "Cannot observe more factors than the ones in the dataset.")
  factors_to_keep = random_state.choice(labels.shape[1],
                                        size=num_observed_factors,
                                        replace=False)
  return labels[:, factors_to_keep], factors_to_keep