混淆矩阵计算准确率代码python
时间: 2023-11-04 20:00:39 浏览: 144
python混淆矩阵代码
混淆矩阵计算准确率的Python代码如下所示:
```python
def calculate_accuracy(confusion_matrix):
correct_predictions = sum(confusion_matrix[i][i] for i in range(len(confusion_matrix)))
total_predictions = sum(sum(row) for row in confusion_matrix)
accuracy = correct_predictions / total_predictions
return accuracy
# 示例混淆矩阵
confusion_matrix = [
[10, 2, 4],
[3, 15, 2],
[1, 4, 12]
]
# 调用函数计算准确率
accuracy = calculate_accuracy(confusion_matrix)
print("准确率: {:.2f}%".format(accuracy * 100))
```
阅读全文