def balance_labels(Y):
    """Compute weights to balance distribution btw 1/0

    Parameters
    ----------
    Y : ndarray
        Binary Label matrix or vector {0, 1}.

    Outputs
    -------
    w_pos : float
        Weight for samples labeled as 1
    w_neg : float
        Weight for samples labeled as 0

    """
    w_pos = Y.sum() * 1.0
    w_neg = Y.size * 1.0 - w_pos

    if w_pos > w_neg:
        w_neg, w_pos = 1.0, w_neg / w_pos
    elif w_pos < w_neg:
        w_neg, w_pos = w_pos / w_neg, 1.0
    else:
        w_pos = w_neg = 1.0
    return w_pos, w_neg