Auc

class paddle.metric. Auc [源代码]

注意:目前只用Python实现Auc,可能速度略慢

该接口计算Auc,在二分类(binary classification)中广泛使用。相关定义参考 https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve

该接口创建四个局部变量true_positives, true_negatives, false_positives和false_negatives,用于计算Auc。为了离散化AUC曲线,使用临界值的线性间隔来计算召回率和准确率的值。用false positive的召回值高度计算ROC曲线面积,用recall的准确值高度计算PR曲线面积。

参数:

  • curve (str) - 将要计算的曲线名的模式,包括’ROC’(默认)或者’PR’(Precision-Recall-curve)。

  • num_thresholds (int) - 离散化AUC曲线的整数阈值数,默认是4095。

  • name (str,可选) – metric实例的名字,默认是’auc’。

代码示例

独立使用示例

  1. import numpy as np
  2. import paddle
  3. m = paddle.metric.Auc()
  4. n = 8
  5. class0_preds = np.random.random(size = (n, 1))
  6. class1_preds = 1 - class0_preds
  7. preds = np.concatenate((class0_preds, class1_preds), axis=1)
  8. labels = np.random.randint(2, size = (n, 1))
  9. m.update(preds=preds, labels=labels)
  10. res = m.accumulate()

在Model API中的示例

  1. import numpy as np
  2. import paddle
  3. import paddle.nn as nn
  4. class Data(paddle.io.Dataset):
  5. def __init__(self):
  6. super(Data, self).__init__()
  7. self.n = 1024
  8. self.x = np.random.randn(self.n, 10).astype('float32')
  9. self.y = np.random.randint(2, size=(self.n, 1)).astype('int64')
  10. def __getitem__(self, idx):
  11. return self.x[idx], self.y[idx]
  12. def __len__(self):
  13. return self.n
  14. model = paddle.Model(nn.Sequential(
  15. nn.Linear(10, 2), nn.Softmax())
  16. )
  17. optim = paddle.optimizer.Adam(
  18. learning_rate=0.001, parameters=model.parameters())
  19. def loss(x, y):
  20. return nn.functional.nll_loss(paddle.log(x), y)
  21. model.prepare(
  22. optim,
  23. loss=loss,
  24. metrics=paddle.metric.Auc())
  25. data = Data()
  26. model.fit(data, batch_size=16)

update(pred, label, *args)

更新AUC计算的状态。

参数

  • preds (numpy.array | Tensor): 一个shape为[batch_size, 2]的Numpy数组或Tensor,preds[i][j]表示第i个样本类别为j的概率。

  • labels (numpy.array | Tensor): 一个shape为[batch_size, 1]的Numpy数组或Tensor,labels[i]是0或1,表示第i个样本的类别。

返回: 无。

reset()

清空状态和计算结果。

返回:无

accumulate()

累积的统计指标,计算和返回AUC值。

返回:AUC值,一个标量。

name()

返回Metric实例的名字, 参考上述的name,默认是’auc’。

返回: 评估的名字,string类型。