如何解决我试图在同一个图上绘制多条 ROC 曲线我使用的是多类数据集,只能得到单独的图
这是我目前的代码。我只能得到单独的 ROC 曲线。有人可以帮忙吗?因为它是一个多类数据集,所以这里的很多解决方案对我都不起作用。无论如何,我是否可以迭代此代码以将其全部放在一个图上
def ROC(model):
#Importing and splitting the datset into train/test
dataset = pd.read_csv("dataset-sat.csv")
features,class_col = Train_Test_split(dataset)
class_col = preprocessing.label_binarize(class_col,classes=[0,1,2])
n_classes = class_col.shape[1]
X_train,X_test,y_train,y_test = train_test_split(features,class_col,test_size=0.30,random_state=0)
#Usig Ovr classifier
classifier = OneVsRestClassifier(model)
y_score = classifier.fit(X_train,y_train).predict_proba(X_test)
print('KNN AUROC score ',round(roc_auc_score(y_test,y_score,multi_class ='ovr' ),4))
#Creating dictionaies for fpr,tpr and AUC to iterate through each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i],tpr[i],_ = roc_curve(y_test[:,i],y_score[:,i])
roc_auc[i] = auc(fpr[i],tpr[i])
# Compute micro-average ROC curve and ROC area
fpr["micro"],tpr["micro"],_ = roc_curve(y_test.ravel(),y_score.ravel())
roc_auc["micro"] = auc(fpr["micro"],tpr["micro"])
#Plotting figure
plt.figure()
lw = 2
plt.plot(fpr[2],tpr[2],color='darkorange',lw=lw,label='ROC curve (area = %0.2f)' % roc_auc[2])
plt.plot([0,1],[0,color='navy',linestyle='--')
plt.xlim([0.0,1.0])
plt.ylim([0.0,1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
classifiers = [KNeighborsClassifier(),GaussianNB(),RandomForestClassifier()]
for i in classifiers:
ROC(i)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。