【问题标题】:ROC curves for multiclass classification in RR中多类分类的ROC曲线
【发布时间】:2016-08-06 11:26:23
【问题描述】:

我有一个包含 6 个类的数据集,我想为多类分类绘制一条 ROC 曲线。 Achim Zeileis 在这个帖子中给出的第一个答案是一个非常好的答案。

ROC curve in R using rpart package?

但这仅适用于二项式分类。我得到的错误是Error in prediction, Number of classes is not equal to 2。有人为多类分类做过这个吗?

这是我正在尝试做的一个简单示例。 数据

假设 data$cType 具有 6 值(或级别)为(red、green、blue、yellow、blackwhite)

有没有为这 6 个类绘制 ROC 曲线?任何超过 2 个类的工作示例将不胜感激。

【问题讨论】:

  • @achim-zeileis,亲爱的有什么建议吗?
  • 您希望在多类分类的 ROC 曲线中显示什么? ROC 曲线旨在显示二元结果;更准确地说是两个比率:真阳性与假阳性。您可以为您的六个案例建立每条这样的曲线,但我看不出如何定义多类分类的单个 ROC 曲线。
  • 我想像对二进制文件一样进行所有性能测量。我已经读到可以使用名为 pROC 的 R 包来实现它,但我找不到一个可行的示例。
  • 在我看来,唯一可行的方法是将问题转换为几个二进制问题,方法是定义六种不同的“一个与其余”案例并评估相应的 ROC 曲线。
  • 我现在用一个简单的例子编辑了我的问题。亲爱的,你能给我一个简单的工作例子吗?

标签: r machine-learning classification roc


【解决方案1】:

我知道这是一个老问题,但唯一的答案是使用 Python 编写的这一事实让我很困扰,因为该问题专门要求 R 解决方案。

从下面的代码可以看出,我使用的是pROC::multiclass.roc() 函数。使其工作的唯一要求是预测矩阵的列名称匹配真正的类 (real_values)。

第一个示例生成随机预测。第二个产生更好的预测。第三个生成完美的预测(即,始终将最高概率分配给真实类。)

library(pROC)
set.seed(42)
head(real_values)
real_values <- matrix( c("class1", "class2", "class3"), nc=1 )

# [,1]    
# [1,] "class1"
# [2,] "class2"
# [3,] "class3"

# Random predictions
random_preds <- matrix(rbeta(3*3,2,2), nc=3)
random_preds <- sweep(random_preds, 1, rowSums(a1), FUN="/")
colnames(random_preds) <- c("class1", "class2", "class3")


head(random_preds)

#       class1    class2    class3
# [1,] 0.3437916 0.6129104 0.4733117
# [2,] 0.6016169 0.4700832 0.9364681
# [3,] 0.6741742 0.8677781 0.4823129

multiclass.roc(real_values, random_preds)
#Multi-class area under the curve: 0.1667



better_preds <- matrix(c(0.75,0.15,0.5,
                         0.15,0.5,0.75,
                         0.15,0.75,0.5), nc=3)
colnames(better_preds) <- c("class1", "class2", "class3")

head(better_preds)

#       class1 class2 class3
# [1,]   0.75   0.15   0.15
# [2,]   0.15   0.50   0.75
# [3,]   0.50   0.75   0.50

multiclass.roc(real_values, better_preds)
#Multi-class area under the curve: 0.6667


perfect_preds <- matrix(c(0.75,0.15,0.5,
                          0.15,0.75,0.5,
                          0.15,0.5,0.75), nc=3)
colnames(perfect_preds) <- c("class1", "class2", "class3")
head(perfect_preds)

multiclass.roc(real_values, perfect_preds)
#Multi-class area under the curve: 1

【讨论】:

    【解决方案2】:

    在具有相同要求的同时回答一个老问题 - 我发现 scikit 文档很好地解释了一些方法。

    http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html

    提到的方法包括:

    • “二值化”,即将问题转换为二值分类,使用宏观平均或微观平均
    • 绘制多条 ROC 曲线,每个标签一条
    • 一对一

    从上面的链接中复制示例,说明一对多和使用它们的库进行微平均:

    print(__doc__)
    
    import numpy as np
    import matplotlib.pyplot as plt
    from itertools import cycle
    
    from sklearn import svm, datasets
    from sklearn.metrics import roc_curve, auc
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import label_binarize
    from sklearn.multiclass import OneVsRestClassifier
    from scipy import interp
    
    # Import some data to play with
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target
    
    # Binarize the output
    y = label_binarize(y, classes=[0, 1, 2])
    n_classes = y.shape[1]
    
    # Add noisy features to make the problem harder
    random_state = np.random.RandomState(0)
    n_samples, n_features = X.shape
    X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
    
    # shuffle and split training and test sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
                                                        random_state=0)
    
    # Learn to predict each class against the other
    classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
                                     random_state=random_state))
    y_score = classifier.fit(X_train, y_train).decision_function(X_test)
    
    # Compute ROC curve and ROC area for 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"])
    

    我实际上正在寻找一个 Javascript 解决方案(使用 https://github.com/mljs/performance),所以我没有使用上述库实现它,但它是迄今为止我发现的最有启发性的示例。

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 2021-07-30
      • 2021-03-03
      • 2021-03-08
      • 2019-02-27
      • 1970-01-01
      • 2018-12-19
      • 2017-08-19
      • 2014-01-03
      相关资源
      最近更新 更多