2020-08-24绘制ROC   PR曲线 核心方法总结 ,计算AUC核心方法

#1   绘制ROC AUC   PR曲线

#核心方法

roc_curve(y_test, preds)  #  preds为概率形式

 

#source code
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve for different classifiers')

plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')

labels = []
preds = model.predict(val_data_1,val_data_2)
y_test = labels_val
print("preds=",preds)
print("y_test=",labels_val)
fpr, tpr, _ = roc_curve(y_test, preds)
correct_prediction = np.equal(np.round(preds), y_test)
print("准确率=",np.mean(correct_prediction))
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=lw, color=colors[idx])
labels.append(" AUC = {}".format(np.round(roc_auc, 4)))
plt.legend(['random AUC = 0.5'] + labels)

 

 

#2 绘制PR曲线

#核心方法

precision_recall_curve(y_test, preds)    #preds为概率形式

#绘制PR曲线
# precision_recall_curve 评价

80858/80858 [==============================] - 71s 879us/step
preds= [[0.47779566]
 [0.10188359]
 [0.9416214 ]
 ...
 [0.35241508]
 [0.08200879]
 [0.8329295 ]]
y_test= [1 0 1 ... 1 0 1]
准确率= 0.5920709617260591
AUC1 is  0.8846388994626855
AUC2 is  0.8846388994626855

Out[104]:

<matplotlib.legend.Legend at 0x7f16bfbd4e80>

In [105]:

 

 
#绘制PR曲线
# precision_recall_curve 评价
from sklearn.metrics import precision_recall_curve
pr, re, _ = precision_recall_curve(y_test, preds)
plt.figure(figsize=(12,8))
plt.plot(re, pr)
plt.title('PR Curve (AUC {})'.format(auc(re, pr)))
plt.xlabel('Recall')
plt.ylabel('Precision')

Out[105]:

Text(0, 0.5, 'Precision')

 

In [108]:

 

 
#灵敏度计算
print("sens=",tpr)
sense = tpr
specificity = 1-fpr
print("specificity 1-fpr=",1-fpr)
plt.figure(figsize=(12,8))
plt.plot(specificity, sense)
plt.title('sensitivity-specificity curve')
plt.xlabel('specificity ')
plt.ylabel('sensitivity')
sens= [0.         0.00198908 0.00212393 ... 1.         1.         1.        ]
specificity 1-fpr= [1.00000000e+00 1.00000000e+00 1.00000000e+00 ... 1.17196656e-04
 3.90655520e-05 0.00000000e+00]

Out[108]:

Text(0, 0.5, 'sensitivity')

In [80]:

#3  计算AUC 核心方法

roc_auc = auc(fpr, tpr)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值