[英]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))
打印(混淆矩陣(?,?))
打印(分類報告(?,?))
基本上有問號的地方應該得到什么?
你的問題很模糊。 你想要混淆矩陣還是分類報告?
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))
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.