def balance_weights(labels):
    """
    Generates a mapping of class weights that will re-weight a training set
    in a balanced way such that weight(label) = len(obs) / freq(label in obs).
    """
    counts = {}
    for l in labels:
        counts[l] = counts.get(l, 0) + 1

    return {l: (len(labels) / counts[l]) for l in counts}