python - 展开混淆矩阵插入信息
问题描述
在我想要的每个表面上,预测的实际数量。我真的不在乎它只是百分比还是数字。我还想用真阳性和假阴性来标记它们。
编码:
sns.heatmap(pd.crosstab(ytest,classifier.predict(xtest)),cmap='Spectral')
plt.xlabel('predicted')
plt.ylabel('actual')
plt.show()
解决方案
我用下面做你想做的事,虽然谷歌搜索也会给你答案
def find_best_threshold(threshold, fpr, tpr):
t = threshold[np.argmax(tpr * (1-fpr))]
### TPR * TNR ---> We are trying to maximize TNR and TPR
print("the maximum value of tpr*(1-fpr)", max(tpr*(1-fpr)), "for threshold", np.round(t,3))
return t
def predict_with_best_thresh(prob,t):
pred=[1 if i>=t else 0 for i in prob ]
return pred
### https://medium.com/@dtuk81/confusion-matrix-visualization-fc31e3f30fea
def conf_matrix_plot(cf_matrix,title):
group_names = ['True Neg','False Pos','False Neg','True Pos']
group_counts = ["{0:0.0f}".format(value) for value in cf_matrix.flatten()]
group_percentages = ["{0:.2%}".format(value) for value in cf_matrix.flatten()/np.sum(cf_matrix)]
labels = [f"{v1}\n{v2}\n{v3}" for v1, v2, vQ3 in zip(group_names,group_counts,group_percentages)]
labels = np.asarray(labels).reshape(2,2)
#sns.set(font_scale=1.5)
sns.heatmap(cf_matrix, annot=labels, fmt='',cmap='coolwarm').set_title(title + ' Confusion Matrix for TFIDF')
plt.xlabel('Actual')
plt.ylabel('Predicted')
from sklearn.metrics import confusion_matrix
import numpy as np
best_t = find_best_threshold(tr_thresholds, train_fpr, train_tpr)
cf_matrix_train = confusion_matrix(y_train, predict_with_best_thresh(y_train_pred[:,1], best_t))
cf_matrix_test = confusion_matrix(y_test, predict_with_best_thresh(y_test_pred[:,1], best_t))
conf_matrix_plot(cf_matrix_train,'Train')
结果:
推荐阅读
- airflow - 基于数据库表的气流 DAG 调度
- nsis - 使用 nsDialog 的 listview 安装文件
- google-apps-marketplace - 同意屏幕上的图标 URL 与您主页上的图标不匹配
- sql - SQL - 仅访问特定列中结构值的第一行
- tags - Gitlab CI:仅为受保护分支上存在的标记提交运行管道作业
- function - 为什么我的代码中的 String.() -> Unit 与 (String) -> Unit 相同?
- git - 使用基于功能分支的 rebase 提交消息修改,然后合并到 master
- ios-simulator - 如何在 Iphone 中使用 frida 模拟触摸事件(已经越狱)
- strapi - 打开strapi / admin页面时如何修复404页面?
- python - 使用用户输入的函数数据框名称字符串获取熊猫数据框