Precision

class paddle.fluid.metrics. Precision ( name=None ) [source]

Precision (also called positive predictive value) is the fraction of relevant instances among the retrieved instances. Refer to https://en.wikipedia.org/wiki/Evaluation_of_binary_classifiers

Noted that this class manages the precision score only for binary classification task.

Parameters

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

Examples

import paddle.fluid as fluid
import numpy as np

metric = fluid.metrics.Precision()

# generate the preds and labels

preds = [[0.1], [0.7], [0.8], [0.9], [0.2],
         [0.2], [0.3], [0.5], [0.8], [0.6]]

labels = [[0], [1], [1], [1], [1],
          [0], [0], [0], [0], [0]]

preds = np.array(preds)
labels = np.array(labels)

metric.update(preds=preds, labels=labels)
numpy_precision = metric.eval()

print("expect precision: %.2f and got %.2f" % ( 3.0 / 5.0, numpy_precision))
update ( preds, labels )

update

Update the precision based on the current mini-batch prediction results .

Parameters
  • preds (numpy.ndarray) – prediction results of current mini-batch, the output of two-class sigmoid function. Shape: [batch_size, 1]. Dtype: ‘float64’ or ‘float32’.

  • labels (numpy.ndarray) – ground truth (labels) of current mini-batch, the shape should keep the same as preds. Shape: [batch_size, 1], Dtype: ‘int32’ or ‘int64’.

eval ( )

eval

Calculate the final precision.

Returns

Results of the calculated Precision. Scalar output with float dtype.

Return type

float

get_config ( )

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 contains the inner states of the metric instance

Return types:

a python dict

reset ( )

reset

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

Parameters

None

Returns

None

Return types:

None