Mah*_*lid 5 r classification machine-learning roc
我有一个包含 6 个类别的数据集,我想绘制多类别分类的 ROC 曲线。Achim Zeileis 给出的第一个答案非常好。
但这仅适用于二项式分类。我得到的错误是Error in prediction, Number of classes is not equal to 2。有人做过多类分类吗?
这是我正在尝试做的一个简单的例子。数据 <- read.csv("colors.csv")
假设data$cType值6(或级别)为(红色、绿色、蓝色、黄色、黑色和白色)
有没有办法为这 6 个类别绘制 ROC 曲线?任何超过 2 类的工作示例将不胜感激。
在具有相同要求的同时回答一个老问题 - 我发现 scikit 文档很好地解释了一些方法。
http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html
提到的方法包括:
从上面的链接复制示例,该示例说明了使用其库的一对多和微平均:
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"])
Run Code Online (Sandbox Code Playgroud)
我实际上正在寻找一个 Javascript 解决方案(使用https://github.com/mljs/performance),所以我还没有使用上面的库来实现它,但它是迄今为止我发现的最具启发性的示例。
| 归档时间: |
|
| 查看次数: |
6093 次 |
| 最近记录: |