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 |
|
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 |
|
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 |
|