> 文章列表 > 晚上要修改的

晚上要修改的

晚上要修改的

from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
from sklearn.metrics import precision_recall_curvefrom sklearn.metrics import confusion_matrix
import numpy as np
# scores长为8,8个分数   eg:[-0.35268316  1.4137208   1.3485073   1.9631556   0.7549048   0.8002088 0.9486606   1.2362641 ]
# masks为网络预测标签,列表格式,长为8,列表中的元素为288x288的数组
# masks_gt 好像为三层列表 长度分别为1,288,288
# labels_gt为一层列表,异常为1,good为0score_map = [-0.35268316, 1.4137208,  1.3485073,  1.9631556,  0.7549048,  0.8002088,0.9486606, 1.2362641 ]
score_map = np.asarray(score_map)
max_score = score_map.max()
min_score = score_map.min()
scores = (score_map - min_score) / (max_score - min_score)
gt_list = [0,1,1,1,0,1,0,1]
# print('scores is',scores,type(scores))print('scores is',scores,type(scores))
## scores is [0. , 0.76274911 ,0.73458934, 1. , 0.47826644, 0.49782911, 0.56193194 ,0.68612171]
fpr, tpr, _ = roc_curve(gt_list,scores)
img_roc_auc = roc_auc_score(gt_list, scores)
print('img_roc_auc is',img_roc_auc)#计算最优阈值
precision, recall, thresholds = precision_recall_curve(gt_list, scores)
a = 2 * precision * recall
b = precision + recall
f1 = np.divide(a, b, out=np.zeros_like(a), where=b != 0)
threshold = thresholds[np.argmax(f1)]
print("threshold is",threshold)#将预测值根据阈值二值化,方便接下来输出混淆矩阵
scoress = np.where(scores >  threshold,1,scores)
scoress = np.where(scoress <=  threshold,0,scoress)tn, fp, fn, tp = confusion_matrix(gt_list,scoress).ravel()
print("tn, fp, fn, tp is",tn, fp, fn, tp)

佳庆纺织