def show_matrix(y_test, y_pred): # 定义一个函数显示混淆矩阵 y_test=y_test.detach().numpy() y_pred=y_pred.detach().numpy() print(y_test) cm = confusion_matrix(y_test,y_pred) # 调用混淆矩阵 plt.title("ANN Confusion Matrix") # 标题 sns.heatmap(cm,annot=True,cmap="Blues",fmt="d",cbar=False) # 热力图设定 plt.show() # 显示混淆矩阵
时间: 2024-04-08 10:34:37 浏览: 159
这段代码定义了一个名为`show_matrix`的函数,用于显示混淆矩阵。下面是完整的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
def show_matrix(y_test, y_pred):
y_test = y_test.detach().numpy()
y_pred = y_pred.detach().numpy()
print(y_test)
cm = confusion_matrix(y_test, y_pred)
plt.title("ANN Confusion Matrix")
sns.heatmap(cm, annot=True, cmap="Blues", fmt="d", cbar=False)
plt.show()
```
该函数的输入参数`y_test`和`y_pred`是模型的真实标签和预测结果。首先,将它们转换为NumPy数组(假设它们是PyTorch张量)。然后,使用`confusion_matrix`函数计算混淆矩阵。接下来,使用Matplotlib和Seaborn绘制热力图来可视化混淆矩阵。最后,使用`plt.show()`显示混淆矩阵。
请确保已经导入所需的库(numpy、matplotlib、seaborn、sklearn.metrics)并将真实标签和预测结果传递给`show_matrix`函数以进行调用。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)