Skip to content

Class weights

compute_class_weights(ohe_array, method='balanced') ¤

Simple wrapper function for scikit learn class_weight function.

The class weights can be used for weighting the loss function on imbalanced data.

Info

NumPy array shape has to be (n_samples, n_classes) like this: (500, 4).

Example
# Compute class weights
cw_loss, cw_fit = compute_class_weights(class_ohe)

# Provide class weights to loss function
model = NeuralNetwork(nclasses, channels=3, loss=categorical_focal_loss(cw_loss))

# Provide class weights to keras fit()
model.train(index_list, epochs=50, class_weights=cw_fit)
Based on Reference

Scikit-learn class_weight function:
https://scikit-learn.org/stable/modules/generated/sklearn.utils.class_weight.compute_class_weight.html

Parameters:

Name Type Description Default
ohe_array numpy.ndarray

NumPy matrix containing the ohe encoded classification.

required
method str

Dictionary or modus, how class weights should be computed.

'balanced'

Returns:

Name Type Description
class_weights_list numpy.ndarray

Class weight list which can be feeded to a loss function.

class_weights_dict dict

Class weight dictionary which can be feeded to train() or keras.model.fit().

Source code in aucmedi/utils/class_weights.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def compute_class_weights(ohe_array, method="balanced"):
    """ Simple wrapper function for scikit learn class_weight function.

    The class weights can be used for weighting the loss function on imbalanced data.

    ???+ info
        NumPy array shape has to be (n_samples, n_classes) like this: (500, 4).

    ???+ example "Example"
        ```python
        # Compute class weights
        cw_loss, cw_fit = compute_class_weights(class_ohe)

        # Provide class weights to loss function
        model = NeuralNetwork(nclasses, channels=3, loss=categorical_focal_loss(cw_loss))

        # Provide class weights to keras fit()
        model.train(index_list, epochs=50, class_weights=cw_fit)
        ```

    ??? abstract "Based on Reference"
        Scikit-learn class_weight function: <br>
        https://scikit-learn.org/stable/modules/generated/sklearn.utils.class_weight.compute_class_weight.html  <br>

    Args:
        ohe_array (numpy.ndarray):  NumPy matrix containing the ohe encoded classification.
        method (str):               Dictionary or modus, how class weights should be computed.

    Returns:
        class_weights_list (numpy.ndarray):     Class weight list which can be feeded to a loss function.
        class_weights_dict (dict):              Class weight dictionary which can be feeded to
                                                [train()][aucmedi.neural_network.model.NeuralNetwork.train]
                                                or keras.model.fit().
    """
    # Obtain sparse categorical array and number of classes
    class_array = np.argmax(ohe_array, axis=-1)
    n_classes = np.unique(class_array)
    # Compute class weights with scikit learn
    class_weights_list = compute_class_weight(class_weight=method,
                                              classes=n_classes, y=class_array)
    # Convert class weight array to dictionary
    class_weights_dict = dict(enumerate(class_weights_list))
    # Return resulting class weights as list and dictionary
    return class_weights_list, class_weights_dict

compute_multilabel_weights(ohe_array, method='balanced') ¤

Function for computing class weights individually for multi-label data.

Class weights can be used for weighting the loss function on imbalanced data. Returned is a class weight list which can be passed to loss functions.

Info

NumPy array shape has to be (n_samples, n_classes) like this: (500, 4).

Based on Reference

Class weight computation is based on Scikit learn class_weight function:
https://scikit-learn.org/stable/modules/generated/sklearn.utils.class_weight.compute_class_weight.html

Parameters:

Name Type Description Default
ohe_array numpy.ndarray

NumPy matrix containing the ohe encoded classification.

required
method str

Dictionary or modus, how class weights should be computed.

'balanced'

Returns:

Name Type Description
class_weights numpy.ndarray

Class weight list which can be fed to a loss function.

Source code in aucmedi/utils/class_weights.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def compute_multilabel_weights(ohe_array, method="balanced"):
    """ Function for computing class weights individually for multi-label data.

    Class weights can be used for weighting the loss function on imbalanced data.
    Returned is a class weight list which can be passed to loss functions.

    ???+ info
        NumPy array shape has to be (n_samples, n_classes) like this: (500, 4).

    ??? abstract "Based on Reference"
        Class weight computation is based on Scikit learn class_weight function: <br>
        https://scikit-learn.org/stable/modules/generated/sklearn.utils.class_weight.compute_class_weight.html  <br>

    Args:
        ohe_array (numpy.ndarray):      NumPy matrix containing the ohe encoded classification.
        method (str):                   Dictionary or modus, how class weights should be computed.

    Returns:
        class_weights (numpy.ndarray):      Class weight list which can be fed to a loss function.
    """
    # Identify number of classes
    n_classes = np.shape(ohe_array)[1]
    # Initialize class weight list
    class_weights = np.empty([n_classes])
    # Compute weight for each class individually
    for i in range(0, n_classes):
        weight = compute_class_weight(class_weight=method, classes=np.array([0,1]),
                                      y=ohe_array[:, i])
        class_weights[i] = weight[1]
    # Return resulting class weight list
    return class_weights

compute_sample_weights(ohe_array, method='balanced') ¤

Simple wrapper function for scikit learn sample_weight function.

The sample weights can be used for weighting the loss function on imbalanced data. Returned sample weight array which can be directly fed to an AUCMEDI DataGenerator.

Info

NumPy array shape has to be (n_samples, n_classes) like this: (500, 4).

Based on Reference

Scikit learn sample_weight function:
https://scikit-learn.org/stable/modules/generated/sklearn.utils.class_weight.compute_sample_weight.html

Parameters:

Name Type Description Default
ohe_array numpy.ndarray

NumPy matrix containing the ohe encoded classification.

required
method str

Dictionary or modus, how class weights should be computed.

'balanced'

Returns:

Name Type Description
sample_weights numpy.ndarray

Sample weight list which can be fed to an AUCMEDI DataGenerator.

Source code in aucmedi/utils/class_weights.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def compute_sample_weights(ohe_array, method="balanced"):
    """ Simple wrapper function for scikit learn sample_weight function.

    The sample weights can be used for weighting the loss function on imbalanced data.
    Returned sample weight array which can be directly fed to an AUCMEDI [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].

    ???+ info
        NumPy array shape has to be (n_samples, n_classes) like this: (500, 4).

    ??? abstract "Based on Reference"
        Scikit learn sample_weight function: <br>
        https://scikit-learn.org/stable/modules/generated/sklearn.utils.class_weight.compute_sample_weight.html <br>

    Args:
        ohe_array (numpy.ndarray):      NumPy matrix containing the ohe encoded classification.
        method (str):                   Dictionary or modus, how class weights should be computed.

    Returns:
        sample_weights (numpy.ndarray):     Sample weight list which can be fed to an AUCMEDI
                                            [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].
    """
    # Compute sample weights with scikit learn
    sample_weights = compute_sample_weight(class_weight=method, y=ohe_array)
    # Return resulting sample weights
    return sample_weights