sklearn.metrics.roc_curve用于多类分类 [英] sklearn.metrics.roc_curve for multiclass classification
问题描述
我想使用
I want to use sklearn.metrics.roc_curve to get the ROC curve for multiclass classification problem. Here gives a solution on how to fit roc to multiclass problem. But I do not understand what the parameter "y_score" mean, what I should provide for this parameter in a multiclass classification problem.
Suppose a scenario like this. There are nine elements labeled from 0 to 8. The first three elements belong to group 0, the last three belong to group 2 and the three elements between belong to group1. 0, 3, 6 are the centers of the groups. I have a pairwise distance matrix. Then, what should I provide for the "y_score" parameter?
from sklearn.metrics import roc_curve, auc
from sklearn.preprocessing import label_binarize
listTrue=[0,0,0,1,1,1,2,2,2] #value j at index i means element i is in group j
y=label_binarize(y,classes=range(2))
#get distmatrix
#distmatrix[i][j] gives the distance between element i and element j
fpr=dict()
tpr=dict()
roc_auc=dict()
fpr["micro"], tpr["micro"], _=roc_curve(y.ravel(),y_score?)
roc_auc=auc(fpr["micor"], tpr["micro"])
First I will answer your question about y_score
. So, y_score
in the example that you mentioned are the predicted (by the classifier) probabilities for the test samples. If you have 2 classes then y_score
will have 2 columns and each of the columns will contain the probability of a sample to belong to this class.
To plot the multi-class ROC use label_binarize
function and the following code. Adjust and change the code depending on your application.
Example using Iris data:
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.metrics import roc_curve, auc
from sklearn.multiclass import OneVsRestClassifier
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]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=0)
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
random_state=0))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
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])
colors = cycle(['blue', 'red', 'green'])
for i, color in zip(range(n_classes), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([-0.05, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic for multi-class data')
plt.legend(loc="lower right")
plt.show()
In this example, you can print the y_score
.
print(y_score)
array([[-3.58459897, -0.3117717 , 1.78242707],
[-2.15411929, 1.11394949, -2.393737 ],
[ 1.89199335, -3.89592195, -6.29685764],
.
.
.
这篇关于sklearn.metrics.roc_curve用于多类分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!