繁体   English   中英

如何打印深度学习 model 中的混淆矩阵?

[英]How can I print the confusion matrix in the deep learning model?

 def inspection_performance(predicted_fraud, test_fraud): Inspect_Rate = [] Precision=[] Recall=[] for i in range(1,100,1): threshold = np.percentile(predicted_fraud, i) precision = np.mean(test_fraud[predicted_fraud > threshold]) recall = sum(test_fraud[predicted_fraud > threshold])/sum(test_fraud) Inspect_Rate.append(100-i) Precision.append(precision) Recall.append(recall) compiled_conf_matrix = pd.DataFrame({ 'İnceleme Oranı':Inspect_Rate, 'Kesinlik':Precision, 'Hatırlama':Recall, }) return compiled_conf_matrix

我无法打印混淆矩阵结果。 然后我想获得确定性、回忆和 f 分数,但我做不到。 这是我的代码指南。 我怎样才能通过编写类似于下面的代码来获得这个?

from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))

例如,我想最后得到这样的结果。

print(classification_report(y_test, y_pred))

                 precision   recall  f1-score   support

           0       0.96      0.68      0.80     37117
           1       0.14      0.67      0.23      2883

    accuracy                           0.68     40000
   macro avg       0.55      0.68      0.52     40000
weighted avg       0.90      0.68      0.76     40000

我必须写别的东西而不是 y_test 和 y_pred。 但是我不知道怎么写。

  • 作为参考,ROC-AUC曲线的代码如下。 这里应该用哪个标签代替 (y_true, y_pred)?

     from matplotlib import pyplot as plt from sklearn.metrics import roc_curve from sklearn.metrics import auc fpr, tpr, _ = roc_curve(test_labels.drop(np.where(np.isnan(y_pred))[0]), np.delete(y_pred, np.where(np.isnan(y_pred))[0])) plt.plot(fpr, tpr, label='ROC curve') plt.plot([0, 1], [0, 1], 'k--', label='Random guess') plt.xlabel('False positive rate') plt.ylabel('True positive rate') plt.title('ROC curve') plt.legend(loc="lower right") plt.show() print('auc: ', auc(fpr, tpr))
  • 打印(混淆矩阵(?,?))

  • 打印(分类报告(?,?))

基本上有问号的地方应该得到什么?

你的问题很模糊。 你想要混淆矩阵还是分类报告?

  1. 关于混淆矩阵,请参考混淆矩阵官方文档 在这里你会做这样的事情:
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_true, y_pred))

在哪里,

  • y_true:ground_truth 标签

  • y_pred:预测标签

现在,在您的情况下,function 有两个参数:predicted_fraud,test_fraud。 test_fraud 是你的ground_truth吗? 必须有两个标签,即欺诈或无欺诈 (1,0),如果是,那么,

from sklearn.metrics import confusion_matrix
print(confusion_matrix(test_fraud.classes, predicted_fraud))
  1. 分类报告请参考官方文档
from sklearn.metrics import classification_report
target_names = ['fraud', 'No fraud']
print(classification_report(test_fraud.classes, predicted_fraud, target_names=target_names))

分类报告将为您提供每个 class(欺诈,无欺诈)的主要分类指标,例如:精度、召回率、f1 分数、准确率等。

此外,还有一个github 链接,它对我也有帮助,希望对你也有帮助。

我解决了这个问题。 应该发生的事情如下。

from sklearn.metrics import confusion_matrix
print(confusion_matrix(test_labels, y_pred.round()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM