微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

展开混淆矩阵插入信息

如何解决展开混淆矩阵插入信息

在我想要的每个表面上,预测的实际数量。 我真的不在乎它只是百分比还是数字。我还想用 True Positive 和 False Negative 标记它们。

代码

sns.heatmap(pd.crosstab(ytest,classifier.predict(xtest)),cmap='Spectral')

plt.xlabel('predicted')

plt.ylabel('actual')

plt.show()

The Confusionsmatrix

解决方法

我用下面做你想做的事,虽然谷歌搜索也会给你答案

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[:,best_t))

conf_matrix_plot(cf_matrix_train,'Train')



结果:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。