Python 如何计算三级ROC_AUC分数

Python 如何计算三级ROC_AUC分数,python,roc,auc,multiclass-classification,Python,Roc,Auc,Multiclass Classification,我有一个数据有3个类标签(0,1,2)。我试着做ROC曲线。并使用pos_label参数实现了这一点 fpr, tpr, thresholds = metrics.roc_curve(Ytest, y_pred_prob, pos_label = 0) 通过将pos_标签更改为0,1,2-我得到了3张图,现在我在计算AUC分数时遇到了问题。 我如何平均3张图,并从中绘制1张图,然后计算Roc_AUC分数。 我犯了这样的错误 指标:roc\U auc\U得分(Ytest,y\U pred\U p

我有一个数据有3个类标签(0,1,2)。我试着做ROC曲线。并使用pos_label参数实现了这一点

fpr, tpr, thresholds = metrics.roc_curve(Ytest, y_pred_prob, pos_label = 0)
通过将pos_标签更改为0,1,2-我得到了3张图,现在我在计算AUC分数时遇到了问题。 我如何平均3张图,并从中绘制1张图,然后计算Roc_AUC分数。 我犯了这样的错误 指标:roc\U auc\U得分(Ytest,y\U pred\U prob)

ValueError:不支持多类格式

请帮帮我

# store the predicted probabilities for class 0
y_pred_prob = cls.predict_proba(Xtest)[:, 0]
#first argument is true values, second argument is predicted probabilities
fpr, tpr, thresholds = metrics.roc_curve(Ytest, y_pred_prob, pos_label = 0)
plt.plot(fpr, tpr)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.title('ROC curve classifier')
plt.xlabel('False Positive Rate (1 - Specificity)')
plt.ylabel('True Positive Rate (Sensitivity)')
plt.grid(True)

plt.grid(真) plt.grid(真) 从上面的代码。生成3条roc曲线。由于多类


我想通过取平均值或平均值,从3以上得到一个roc曲线。然后,从中得出一个roc_auc分数。

对于多类别,计算每个类别的AUROC通常很有用。例如,下面是我用来分别计算每个类的AUROC的代码的摘录,其中label_的意思是一个字符串列表,描述每个标签是什么,并且各种数组的格式使得每行是不同的示例,每列对应不同的标签:

如果要绘制跨越三个类的平均AUC曲线:此代码包括计算平均AUC的部分,以便可以绘制曲线(如果有三个类,它将绘制三个类的平均AUC)

如果你只想得到三个班级的平均AUC:一旦你分别计算了每个班级的AUC,你就可以平均这三个数字,得到一个整体AUC


如果您想了解更多关于AUROC的背景知识,以及如何计算单个类与多个类的AUROC,您可以参阅本文

多类AUC中的亮点:

不能为所有类计算公共AUC。必须分别计算每个类的AUC。正如您必须计算召回率一样,在进行多类别分类时,每个类别的精度是不同的

计算单个类别AUC的最简单方法:

  • 我们选择一个分类器
  • 来自sklearn.linear\u模型导入逻辑回归

    LRE = LogisticRegression(solver='lbfgs')
    
    LRE.fit(X_train, y_train)
    
  • 我正在列一张多班级的名单

    d=y\u test.unique()

    class\u name=list(d.flatte())

    class\u名称

  • 现在分别计算每个类的AUC

    对于类中的p\u名称:

         `fpr, tpr, thresholds = metrics.roc_curve(y_test,  
                         LRE.predict_proba(X_test)[:,1], pos_label = p) 
    
          auroc = round(metrics.auc(fpr, tpr),2)
          print('LRE',p,'--AUC--->',auroc)`
    

  • 您可能希望在与此主题更相关的地方提出问题,例如是否希望获得更快的答复。
    for label_number in range(len(label_meanings)):
        which_label = label_meanings[label_number] #descriptive string for the label
        true_labels = true_labels_array[:,label_number]
        pred_probs = pred_probs_array[:,label_number]
       #AUROC and AP (sliding across multiple decision thresholds)
        fpr, tpr, thresholds = sklearn.metrics.roc_curve(y_true = true_labels,
                                         y_score = pred_probs,
                                         pos_label = 1)
        auc = sklearn.metrics.auc(fpr, tpr)
    
    LRE = LogisticRegression(solver='lbfgs')
    
    LRE.fit(X_train, y_train)
    
         `fpr, tpr, thresholds = metrics.roc_curve(y_test,  
                         LRE.predict_proba(X_test)[:,1], pos_label = p) 
    
          auroc = round(metrics.auc(fpr, tpr),2)
          print('LRE',p,'--AUC--->',auroc)`