## 附代码!参考了网上资料进行整合,侵权请联系我!
# 输入信息:
model_stats(test_y,prediction,model_name,dir_name,class_labels)
#test_y 测试集合的实际分类信息
prediction 测试集合模型预测分类信息
model_name 模型名字,用于存储结果用
dir_name 结果文件存放路径
class_labels 分类信息,如果是0 ,1两分类,class_labels=[0,1]即可
结果实例:
AUC曲线:
#代码:
def Find_Optimal_Cutoff(TPR, FPR, threshold): #利用约登指数 求最佳阈值
y = TPR - FPR
Youden_index = np.argmax(y) # Only the first occurrence is returned.
optimal_threshold = threshold[Youden_index]
point = [FPR[Youden_index], TPR[Youden_index]]
return optimal_threshold, point
def ROC(label, y_prob):
fpr, tpr, thresholds = metrics.roc_curve(label, y_prob)
roc_auc = metrics.auc(fpr, tpr)
optimal_th, optimal_point = Find_Optimal_Cutoff(TPR=tpr, FPR=fpr, threshold=thresholds)
return fpr, tpr, roc_auc, optimal_th, optimal_point
def ROC_pic(test_y,prediction,model_name,dir_name):
fpr, tpr, roc_auc, optimal_th, optimal_point = ROC(test_y, prediction)
aucplot=dir_name+'/'+model_name+'_auc.png'
auctxt=dir_name+'/'+model_name+'_auc.csv'
safe_open(auctxt,'w').write('AUC: %s'%(roc_auc))#a Tainj
plt.figure(figsize=(7,7))
plt.plot(fpr, tpr, color='darkorange',
lw=2, label='ROC curve (area = %0.2f)' % roc_auc) ###假正率为横坐标,真正率为纵坐标做曲线
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.plot(optimal_point[0], optimal_point[1], marker='o', color='r')
plt.text(optimal_point[0], optimal_point[1], f'Threshold:{optimal_th:.2f}')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.savefig(aucplot, format='png')
plt.show()
##
def model_stats(test_y,prediction,model_name,dir_name,class_labels):
tmp_name=model_name+'_report.csv'
report_name=os.path.join(dir_name,tmp_name)
report=classification_report(test_y,prediction,output_dict=True)
df = pd.DataFrame(report).transpose()
df.to_csv(report_name, index= True)
if(len(Labels)==2):
print("class is 2,AUC")
ROC_pic(test_y,prediction,model_name,dir_name)
cm=confusion_matrix(test_y,prediction)