把confusion_matrix函数的python源码给我
时间: 2023-06-09 12:07:16 浏览: 97
以下是 `confusion_matrix` 函数的 Python 源代码:
```python
def confusion_matrix(actual, predicted, classes):
"""
Calculate confusion matrix and store result as a dictionary.
Parameters:
actual (list): List of actual labels.
predicted (list): List of predicted labels.
classes (list): List of all possible labels.
Returns:
dict: Confusion matrix as dictionary.
"""
matrix = {}
for i in range(len(classes)):
row = {}
for j in range(len(classes)):
row[classes[j]] = 0
matrix[classes[i]] = row
for i in range(len(actual)):
if actual[i] in classes and predicted[i] in classes:
matrix[actual[i]][predicted[i]] += 1
return matrix
```
这是一个计算混淆矩阵的函数,它接受三个参数:`actual`、`predicted` 和 `classes`。 `actual` 和 `predicted` 是两个包含实际和预测标签的列表,而 `classes` 是所有可能标签的列表。函数通过遍历实际标签和预测标签,计算混淆矩阵并将结果存储在一个字典中,最后返回该字典。
阅读全文