from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics # 绘制ROC曲线
# 测试集,pre表示预测的概率,pre2表示阈值为0.5的分类结果(0或1),true表示真实结果
test = pd.DataFrame(result.predict(x_test),columns=['pre'])
test['true'] = y_test
test['pre2'] = test['pre'].apply(lambda x: 1 if x>=0.5 else 0)
train = pd.DataFrame(result.predict(x_train),columns=['pre'])
train['true'] = y_train
train['pre2'] = train['pre'].apply(lambda x: 1 if x>=0.5 else 0)
# 画ROC曲线
fpr_test,tpr_test,th_test = metrics.roc_curve(test.true,test.pre) # 测试集
fpr_train,tpr_train,th_train = metrics.roc_curve(train.true,train.pre) # 训练集
plt.figure(dpi=150,figsize=(3,3))
plt.plot(fpr_test,tpr_test,'b--',label=f'测试集AUC={round(metrics.auc(fpr_test,tpr_test),4)}')
plt.plot(fpr_train,tpr_train,'r--',label=f'训练集AUC={round(metrics.auc(fpr_train,tpr_train),4)}')
plt.legend();plt.title('ROC曲线')
python画ROC曲线
最新推荐文章于 2024-08-22 08:51:11 发布