MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / compute_average_precision

Function compute_average_precision

monai/metrics/average_precision.py:116–184  ·  view source on GitHub ↗

Computes Average Precision (AP). AP is a useful metric to evaluate a classifier when the classes are imbalanced. It summarizes a Precision-Recall according to equation :eq:`ap`. Referring to: `sklearn.metrics.average_precision_score <https://scikit-learn.org/stable/modules/generated/skle

(
    y_pred: torch.Tensor, y: torch.Tensor, average: Average | str = Average.MACRO
)

Source from the content-addressed store, hash-verified

114
115
116def compute_average_precision(
117 y_pred: torch.Tensor, y: torch.Tensor, average: Average | str = Average.MACRO
118) -> np.ndarray | float | npt.ArrayLike:
119 """Computes Average Precision (AP). AP is a useful metric to evaluate a classifier when the classes are
120 imbalanced. It summarizes a Precision-Recall according to equation :eq:`ap`.
121 Referring to: `sklearn.metrics.average_precision_score
122 <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.average_precision_score>`_.
123
124 Args:
125 y_pred: input data to compute, typical classification model output.
126 the first dim must be batch, if multi-classes, it must be in One-Hot format.
127 for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data.
128 y: ground truth to compute AP metric, the first dim must be batch.
129 if multi-classes, it must be in One-Hot format.
130 for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data.
131 average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``}
132 Type of averaging performed if not binary classification.
133 Defaults to ``"macro"``.
134
135 - ``"macro"``: calculate metrics for each label, and find their unweighted mean.
136 This does not take label imbalance into account.
137 - ``"weighted"``: calculate metrics for each label, and find their average,
138 weighted by support (the number of true instances for each label).
139 - ``"micro"``: calculate metrics globally by considering each element of the label
140 indicator matrix as a label.
141 - ``"none"``: the scores for each class are returned.
142
143 Raises:
144 ValueError: When ``y_pred`` dimension is not one of [1, 2].
145 ValueError: When ``y`` dimension is not one of [1, 2].
146 ValueError: When ``average`` is not one of ["macro", "weighted", "micro", "none"].
147
148 Note:
149 Average Precision expects y to be comprised of 0's and 1's. `y_pred` must be either prob. estimates or confidence values.
150
151 """
152 y_pred_ndim = y_pred.ndimension()
153 y_ndim = y.ndimension()
154 if y_pred_ndim not in (1, 2):
155 raise ValueError(
156 f"Predictions should be of shape (batch_size, num_classes) or (batch_size, ), got {y_pred.shape}."
157 )
158 if y_ndim not in (1, 2):
159 raise ValueError(f"Targets should be of shape (batch_size, num_classes) or (batch_size, ), got {y.shape}.")
160 if y_pred_ndim == 2 and y_pred.shape[1] == 1:
161 y_pred = y_pred.squeeze(dim=-1)
162 y_pred_ndim = 1
163 if y_ndim == 2 and y.shape[1] == 1:
164 y = y.squeeze(dim=-1)
165
166 if y_pred_ndim == 1:
167 return _calculate(y_pred, y)
168
169 if y.shape != y_pred.shape:
170 raise ValueError(f"data shapes of y_pred and y do not match, got {y_pred.shape} and {y.shape}.")
171
172 average = look_up_option(average, Average)
173 if average == Average.MICRO:

Callers 2

test_valueMethod · 0.90
aggregateMethod · 0.85

Calls 4

look_up_optionFunction · 0.90
sumFunction · 0.85
_calculateFunction · 0.70
flattenMethod · 0.45

Tested by 1

test_valueMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…