python中如何输出混淆矩阵
时间: 2024-05-28 08:03:32 浏览: 243
输入TP,TN,FP和FN,然后输出混淆矩阵和评价指标的Python代码
5星 · 资源好评率100%
要在Python中输出混淆矩阵,可以使用numpy库来实现。首先,我们需要确定输出和标签。可以使用numpy数组来表示真实标签和预测标签。根据引用中的代码,我们可以创建一个长度为100的y_true数组,其中包含了20个"A",20个"B",20个"C",20个"D"和20个"E",代码如下:
```python
import numpy as np
y_true = np.array(["A"]*20 + ["B"]*20 + ["C"]*20 + ["D"]*20 + ["E"]*20)
```
接下来,我们可以创建一个混淆矩阵,并根据预测结果对其进行赋值。根据引用中的代码,我们可以使用numpy的zeros函数创建一个全零矩阵,然后根据预测结果将对应位置赋值为1,代码如下:
```python
confusion_matrix = np.zeros((5, 5))
list_pred_label = [["A", "B"], ["B", "C"], ["C", "D"], ["D", "E"], ["E", "A"]]
for i in list_pred_label:
confusion_matrix[i<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [python实现混淆矩阵(numpy)](https://blog.csdn.net/qq_41375318/article/details/127429930)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [利用python中的matplotlib打印混淆矩阵实例](https://download.csdn.net/download/weixin_38741759/12850834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文