R中用于多类分类的ROC曲线 [英] ROC curves for multiclass classification in R

查看:491
本文介绍了R中用于多类分类的ROC曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含6类的数据集,我想为多类分类绘制ROC曲线.Achim Zeileis在此主题中给出的第一个答案是一个很好的答案.

I have a dataset with 6 classes and I would like to plot a ROC curve for a multiclass classification. The first answer in this thread given by Achim Zeileis is a very good one.

使用rpart包在R中的ROC曲线?

但这仅适用于二项式分类.我得到的错误是预测错误,类数不等于2 .有人为此做过多类分类吗?

But this works only for a binomial classification. And the error i get is Error in prediction, Number of classes is not equal to 2. Any one who has done this for a multi-class classification?

这是我要执行的操作的一个简单示例.数据<-read.csv("colors.csv")

Here is a simple example of what I am trying to do. data <- read.csv("colors.csv")

让我们说 data $ cType 具有 6 值(或级别)为(红色,绿色,蓝色,黄色,黑色白色)

是否有针对这6个类别绘制ROC曲线?任何超过2类的工作示例都将受到赞赏.

Is there anyway to plot a ROC curve for these 6 classes? Any working example for a class of more than 2 would be appreciated.

推荐答案

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

Answering an old question while having the same requirement - I've found the scikit documentation explains a few approaches well.

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

提到的方法包括:

  • 二进制化",即使用宏平均或微观平均将问题转换为二进制分类
  • 绘制多条ROC曲线,每个标签一条
  • 一个vs.全部

从上面的链接复制示例,该示例说明了一个vs.全部以及使用它们的库进行微平均:

Copying example from the above link, which illustrates one vs. all and micro averaging using their libs:

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 ),因此我尚未在上述库中实现它,但这是迄今为止我发现的最有启发性的示例.

I'm actually looking for a Javascript solution (using https://github.com/mljs/performance) so I haven't implemented it with the above library, but it's been the most illuminating example I found so far.

这篇关于R中用于多类分类的ROC曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆