class paddle.metric. Precision ( name='precision', *args, **kwargs ) [source]

Precision (also called positive predictive value) is the fraction of relevant instances among the retrieved instances. Refer to

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


name (str, optional) – String name of the metric instance. Default is precision.

Example by standalone:

import numpy as np
import paddle

x = np.array([0.1, 0.5, 0.6, 0.7])
y = np.array([0, 1, 1, 1])

m = paddle.metric.Precision()
m.update(x, y)
res = m.accumulate()
print(res) # 1.0

Example with Model API:

import numpy as np

import paddle
import paddle.nn as nn

class Data(
    def __init__(self):
        super(Data, self).__init__()
        self.n = 1024
        self.x = np.random.randn(self.n, 10).astype('float32')
        self.y = np.random.randint(2, size=(self.n, 1)).astype('float32')

    def __getitem__(self, idx):
        return self.x[idx], self.y[idx]

    def __len__(self):
        return self.n

model = paddle.Model(nn.Sequential(
    nn.Linear(10, 1),
optim = paddle.optimizer.Adam(
    learning_rate=0.001, parameters=model.parameters())

data = Data(), batch_size=16)
update ( preds, labels )

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

  • preds (numpy.ndarray) – The prediction result, usually the output of two-class sigmoid function. It should be a vector (column vector or row vector) with data type: ‘float64’ or ‘float32’.

  • labels (numpy.ndarray) – The ground truth (labels), the shape should keep the same as preds. The data type is ‘int32’ or ‘int64’.

reset ( )

Resets all of the metric state.

accumulate ( )

Calculate the final precision.


results of the calculated precision.

Return type

A scaler float

name ( )

Returns metric name

compute ( *args )

This API is advanced usage to accelerate metric calculating, calulations from outputs of model to the states which should be updated by Metric can be defined here, where Paddle OPs is also supported. Outputs of this API will be the inputs of “Metric.update”.

If compute is defined, it will be called with outputs of model and labels from data as arguments, all outputs and labels will be concatenated and flatten and each filed as a separate argument as follows: compute(output1, output2, ..., label1, label2,...)

If compute is not defined, default behaviour is to pass input to output, so output format will be: return output1, output2, ..., label1, label2,...

see Metric.update