簡體   English   中英

R中多類分類的ROC曲線

[英]ROC curves for multiclass classification in R

我有一個包含 6 個類的數據集,我想為多類分類繪制 ROC 曲線。 Achim Zeileis 在這個線程中給出的第一個答案是一個非常好的答案。

使用rpart包的R中的ROC曲線?

但這僅適用於二項式分類。 我得到的Error in prediction, Number of classes is not equal to 2 有沒有人為多類分類做過這個?

這是我正在嘗試做的一個簡單示例。 數據 <- read.csv("colors.csv")

假設data$cType6值(或級別)為(紅色、綠色、藍色、黃色、黑色白色

有沒有辦法為這 6 個類繪制 ROC 曲線? 任何超過 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 ),所以我沒有用上面的庫實現它,但它是我迄今為止發現的最有啟發性的例子。

我知道這是一個老問題,但鑒於該問題專門要求 R 解決方案,因此唯一的答案是使用 Python 編寫的這一事實讓我很困擾。

從下面的代碼中可以看出,我正在使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM