(y_true, scale_by_total=True)
return weights, class_counts
Compute class weights from class label counts.
Compute class weights from class label counts.
Compute class weights from class label counts .
def balanced_class_weights(y_true, scale_by_total=True):
    """Compute class weights from class label counts."""
    y = y_true.astype(np.bool)
    nP = y.sum()
    nN = y.size - y.sum()
    class_counts = np.array([nN, nP])

    weights = np.ones_like(y, dtype=float)
    weights[ y] /= nP
    weights[~y] /= nN
    if scale_by_total:
        weights *= y.size

    return weights, class_counts
def balanced_class_weights ( y_true , scale_by_total = True ) : y = y_true . astype ( np . bool ) nP = y . sum ( ) nN = y . size - y . sum ( ) class_counts = np . array ( [ nN , nP ] ) weights = np . ones_like ( y , dtype = float ) weights [ y ] /= nP weights [ ~ y ] /= nN if scale_by_total : weights *= y . size return weights , class_counts