计算准确率和召回率的Python代码
时间: 2023-12-25 17:55:18 浏览: 61
python 精准率和召回率
可以使用Scikit-learn库来计算准确率和召回率。下面是一个简单的示例代码:
```python
from sklearn.metrics import accuracy_score, recall_score
y_true = [0, 1, 1, 0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1, 0, 1, 1]
accuracy = accuracy_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
print("Accuracy:", accuracy)
print("Recall:", recall)
```
输出:
```
Accuracy: 0.625
Recall: 0.6666666666666666
```
其中,`y_true`表示正确的标签,`y_pred`表示模型预测的标签。`accuracy_score`函数计算准确率,`recall_score`函数计算召回率。
阅读全文