#真实y,预测的类别和概率
import numpy as np
y_test_np = np.array(y_test])
y_pred_np = np.array(y_pred)
y_prob_np = np.array(y_prob)
###ROC曲线###
from matplotlib import rcParams
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="C:/WINDOWS/Fonts/STSONG.TTF")
# 设置字体类型,满足一些期刊要求的Times New Roman或与正文字体一致
config = {
"font.family":'Times New Roman',
"font.size":10
# "mathtext.fontset":'stix',
}
rcParams.update(config)
fpr,tpr,threshold = roc_curve(y_test_np, y_prob_np) ###计算真正率和假正率
roc_auc = auc(fpr,tpr) #曲线下面积
plt.figure()
lw = 3.4
plt.figure(figsize=(12,10)) #画布大小
plt.plot(fpr, tpr, ###假正率为横坐标,真正率为纵坐标做曲线
color='red', #曲线颜色
lw=lw, #线的粗细
label='AUROC = %0.4f' % roc_auc,
linestyle='--') #曲线类型
plt.xlim([-0.05, 1.05]) #X轴范围
plt.ylim([-0.05, 1.05]) #Y轴范围
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.xlabel('False positive rate',fontsize=18)
plt.ylabel('True positive rate',fontsize=18)
plt.title('ROC curve',fontsize=22)
plt.legend(loc="lower right",fontsize=18)
#设置图框线粗细
bwith = 1.0 #边框宽度设置为2
TK = plt.gca()#获取边框
TK.spines['bottom'].set_linewidth(bwith)#图框下边
TK.spines['left'].set_linewidth(bwith)#图框左边
TK.spines['top'].set_linewidth(bwith)#图框上边
TK.spines['right'].set_linewidth(bwith)#图框右边
plt.savefig('$path/roc.tiff', dpi=600) #保存图片,设置分辨率
plt.show()
###PRC曲线###
from sklearn.metrics import precision_recall_curve, average_precision_score
from matplotlib import rcParams
config = {
"font.family":'Times New Roman', # 设置字体类型
"font.size":20
# "mathtext.fontset":'stix',
}
rcParams.update(config)
precision, recall, thresholds = precision_recall_curve(y_test_np, y_prob_np)
roc_prc = average_precision_score(y_test_np, y_prob_np)
plt.figure(figsize=(10,8))
lw = 2
plt.plot(recall,precision,
color='blue',
lw=lw,
label=' AUPRC = %0.4f' % roc_prc,
linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.xlabel('Recall',fontsize=10)
plt.ylabel('Precision',fontsize=10)
plt.title(' PRC curve',fontsize=12)
plt.legend(loc="lower right",fontsize=10)
#设置图框线粗细
bwith = 1.0 #边框宽度设置为2
TK = plt.gca()#获取边框
TK.spines['bottom'].set_linewidth(bwith)#图框下边
TK.spines['left'].set_linewidth(bwith)#图框左边
TK.spines['top'].set_linewidth(bwith)#图框上边
TK.spines['right'].set_linewidth(bwith)#图框右边
plt.savefig('$path/prc.tiff', dpi=600)
plt.show()
10-11
6345
04-01
969
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交