tensorflow生成混淆矩阵ROC曲线的库
时间: 2023-08-21 15:05:00 浏览: 48
可以使用sklearn库中的metrics模块来生成混淆矩阵和ROC曲线。
下面是一个示例代码,可以根据自己的需要进行修改:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, roc_curve, auc
# 生成随机数据
y_true = np.random.randint(2, size=100)
y_pred = np.random.randint(2, size=100)
# 计算混淆矩阵
confusion = confusion_matrix(y_true, y_pred)
# 绘制混淆矩阵
plt.imshow(confusion, cmap=plt.cm.Blues)
plt.title("Confusion matrix")
plt.colorbar()
tick_marks = np.arange(2)
plt.xticks(tick_marks, ["Negative", "Positive"])
plt.yticks(tick_marks, ["Negative", "Positive"])
plt.xlabel("Predicted label")
plt.ylabel("True label")
plt.show()
# 计算ROC曲线和AUC值
fpr, tpr, thresholds = roc_curve(y_true, y_pred)
roc_auc = auc(fpr, tpr)
# 绘制ROC曲线
plt.plot(fpr, tpr, label="ROC curve (area = %0.2f)" % roc_auc)
plt.plot([0, 1], [0, 1], "k--")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title("Receiver operating characteristic example")
plt.legend(loc="lower right")
plt.show()
```
运行后,会生成混淆矩阵和ROC曲线的图表。