Auc

class paddle.fluid.metrics.Auc(name, curve='ROC', num_thresholds=4095)[source]

The auc metric is for binary classification. Refer to https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve. Please notice that the auc metric is implemented with python, which may be a little bit slow. If you concern the speed, please use the fluid.layers.auc instead.

The auc function creates four local variables, true_positives, true_negatives, false_positives and false_negatives that are used to compute the AUC. To discretize the AUC curve, a linearly spaced set of thresholds is used to compute pairs of recall and precision values. The area under the ROC-curve is therefore computed using the height of the recall values by the false positive rate, while the area under the PR-curve is the computed using the height of the precision values by the recall.

Parameters
  • name (str, optional) – Metric name. For details, please refer to Name. Default is None.

  • curve (str) – Specifies the name of the curve to be computed, ‘ROC’ [default] or ‘PR’ for the Precision-Recall-curve.

“NOTE: only implement the ROC curve type via Python now.”

Examples

import paddle.fluid as fluid
import numpy as np
# init the auc metric
auc_metric = fluid.metrics.Auc("ROC")

# suppose that batch_size is 128
batch_num = 100
batch_size = 128

for batch_id in range(batch_num):

    class0_preds = np.random.random(size = (batch_size, 1))
    class1_preds = 1 - class0_preds

    preds = np.concatenate((class0_preds, class1_preds), axis=1)

    labels = np.random.randint(2, size = (batch_size, 1))
    auc_metric.update(preds = preds, labels = labels)

    # shall be some score closing to 0.5 as the preds are randomly assigned
    print("auc for iteration %d is %.2f" % (batch_id, auc_metric.eval()))
update(preds, labels)

Update the auc curve with the given predictions and labels.

Parameters
  • preds (numpy.array) – an numpy array in the shape of (batch_size, 2), preds[i][j] denotes the probability of classifying the instance i into the class j.

  • labels (numpy.array) – an numpy array in the shape of (batch_size, 1), labels[i] is either o or 1, representing the label of the instance i.

eval()

Return the area (a float score) under auc curve

Returns

the area under auc curve

Return type

float

get_config()

Get the metric and current states. The states are the members who do not has “_” prefix.

Parameters

None

Returns

a python dict, which costains the inner states of the metric instance

Return types:

a python dict

reset()

reset function empties the evaluation memory for previous mini-batches.

Parameters

None

Returns

None

Return types:

None