可以计算准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure的代码
时间: 2024-01-03 20:04:57 浏览: 179
MATLAB 计算相交面积
以下是Python的示例代码,用于计算准确率、精确率、召回率和F1-Measure的值:
```python
# 实际标签和预测标签的数组
y_true = [1, 0, 1, 1, 0, 1, 1, 0, 1, 0]
y_pred = [1, 1, 1, 0, 0, 1, 0, 1, 0, 0]
# 计算TP、TN、FP、FN
TP = sum([1 for i in range(len(y_true)) if y_true[i] == 1 and y_pred[i] == 1])
TN = sum([1 for i in range(len(y_true)) if y_true[i] == 0 and y_pred[i] == 0])
FP = sum([1 for i in range(len(y_true)) if y_true[i] == 0 and y_pred[i] == 1])
FN = sum([1 for i in range(len(y_true)) if y_true[i] == 1 and y_pred[i] == 0])
# 计算准确率
accuracy = (TP + TN) / (TP + TN + FP + FN)
print("Accuracy:", accuracy)
# 计算精确率
precision = TP / (TP + FP)
print("Precision:", precision)
# 计算召回率
recall = TP / (TP + FN)
print("Recall:", recall)
# 计算F1-Measure
f1_measure = 2 * precision * recall / (precision + recall)
print("F1-Measure:", f1_measure)
```
在上面的示例代码中,`y_true`和`y_pred`分别表示实际标签和预测标签的数组。通过计算TP、TN、FP、FN来计算准确率、精确率、召回率和F1-Measure的值。最后,将计算的结果打印出来。
阅读全文