python根据4*4的混淆矩阵cm计算查准率、查全率和F1-score
时间: 2024-03-23 19:37:32 浏览: 151
Python 计算混淆矩阵
可以使用以下代码来计算查准率、查全率和F1-score:
```python
import numpy as np
cm = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
# 计算真正例(True Positive)
tp = np.diag(cm)
# 计算假正例(False Positive)
fp = np.sum(cm, axis=0) - tp
# 计算假反例(False Negative)
fn = np.sum(cm, axis=1) - tp
# 计算查准率(Precision)
precision = tp / (tp + fp)
# 计算查全率(Recall)
recall = tp / (tp + fn)
# 计算F1-score
f1_score = 2 * precision * recall / (precision + recall)
print("查准率:", precision)
print("查全率:", recall)
print("F1-score:", f1_score)
```
请将上述代码中的 `cm` 数组替换为你的混淆矩阵即可。
阅读全文