精确度和召回率的代码
时间: 2024-03-22 14:32:52 浏览: 161
准确率和召回率matlab代码-SPBL_Pytorch:PyTorch实施“用于临床皮肤病识别的自动平衡学习”
精确度(Precision)和召回率(Recall)是用于评估分类模型性能的常用指标。下面是计算精确度和召回率的代码示例:
```python
# 示例数据:预测结果和真实标签
predictions = [1, 0, 1, 1, 0, 1]
labels = [1, 0, 1, 0, 1, 0]
# 计算真正例数量
true_positives = sum([1 for p, l in zip(predictions, labels) if p == l and p == 1])
# 计算假正例数量
false_positives = sum([1 for p, l in zip(predictions, labels) if p != l and p == 1])
# 计算真负例数量
true_negatives = sum([1 for p, l in zip(predictions, labels) if p == l and p == 0])
# 计算假负例数量
false_negatives = sum([1 for p, l in zip(predictions, labels) if p != l and p == 0])
# 计算精确度
precision = true_positives / (true_positives + false_positives)
# 计算召回率
recall = true_positives / (true_positives + false_negatives)
# 打印结果
print("Precision: ", precision)
print("Recall: ", recall)
```
阅读全文