def balanced_sample(
    sample_collection,
    k,
    path,
    tag=None,
    exact=True,
    seed=None,
):
    """Generates a random sample of size ``k`` from the given collection such
    that the expected histogram of ``path`` values in the sample is uniform.

    Example::

        import fiftyone as fo
        import fiftyone.utils.random as four
        import fiftyone.zoo as foz
        from fiftyone import ViewField as F

        dataset = foz.load_zoo_dataset("cifar10", split="train")

        # Sample proportional to label length
        weights = dataset.values(F("ground_truth.label").strlen())
        view1 = four.weighted_sample(dataset, 10000, weights)

        # Now take a balanced sample from this unbalanced sample
        view2 = four.balanced_sample(view1, 2000, "ground_truth.label")

        # Plot results
        plot1 = fo.CategoricalHistogram("ground_truth.label", init_view=dataset)
        plot2 = fo.CategoricalHistogram(
            "ground_truth.label",
            order=lambda kv: -len(kv[0]),  # order by label length
            init_view=view1,
        )
        plot3 = fo.CategoricalHistogram("ground_truth.label", init_view=view2)
        plot = fo.ViewGrid([plot1, plot2, plot3])
        plot.show()

    Args:
        sample_collection: a :class:`fiftyone.core.collections.SampleCollection`
        k: the number of samples to select
        path: the categorical field against which to sample, e.g.,
            ``"ground_truth.label"``
        tag (None): an optional sample tag to use to encode the results
        exact (True): whether to tag exactly ``k`` samples (True) or sample so
            that the expected number of samples is ``k`` (False)
        seed (None): an optional random seed to use

    Returns:
        a :class:`fiftyone.core.view.DatasetView` containing the sample
    """
    counts = sample_collection.count_values(path)
    weights = np.array([1 / counts[l] for l in sample_collection.values(path)])

    return weighted_sample(
        sample_collection,
        k,
        weights,
        tag=tag,
        exact=exact,
        seed=seed,
    )
Generates a random sample of size ``k`` from the given collection such
    that the expected histogram of ``path`` values in the sample is uniform.

    Example::

        import fiftyone as fo
        import fiftyone.utils.random as four
        import fiftyone.zoo as foz
        from fiftyone import ViewField as F

        dataset = foz.load_zoo_dataset("cifar10", split="train")

        # Sample proportional to label length
        weights = dataset.values(F("ground_truth.label").strlen())
        view1 = four.weighted_sample(dataset, 10000, weights)

        # Now take a balanced sample from this unbalanced sample
        view2 = four.balanced_sample(view1, 2000, "ground_truth.label")

        # Plot results
        plot1 = fo.CategoricalHistogram("ground_truth.label", init_view=dataset)
        plot2 = fo.CategoricalHistogram(
            "ground_truth.label",
            order=lambda kv: -len(kv[0]),  # order by label length
            init_view=view1,
        )
        plot3 = fo.CategoricalHistogram("ground_truth.label", init_view=view2)
        plot = fo.ViewGrid([plot1, plot2, plot3])
        plot.show()

    Args:
        sample_collection: a :class:`fiftyone.core.collections.SampleCollection`
        k: the number of samples to select
        path: the categorical field against which to sample, e.g.,
            ``"ground_truth.label"``
        tag (None): an optional sample tag to use to encode the results
        exact (True): whether to tag exactly ``k`` samples (True) or sample so
            that the expected number of samples is ``k`` (False)
        seed (None): an optional random seed to use

    Returns:
        a :class:`fiftyone.core.view.DatasetView` containing the sample
    
fiftyone/utils/random.py