Generating ROC curve using python for multiclassification
Following from here: Convert 1D array to matrix based on 2D class in python
I want to draw ROC curves for each of my 46 classes. I have 300 test cases that I run my classifier for to make a prediction.
y_test
- these are true classes, and y_pred
- this is what my classifier predicted.
Here's my code:
from sklearn.metrics import confusion_matrix, roc_curve, auc
from sklearn.preprocessing import label_binarize
import numpy as np
y_test_bi = label_binarize(y_test, classes=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19,20,21,2,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,3,40,41,42,43,44,45])
y_pred_bi = label_binarize(y_pred, classes=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19,20,21,2,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,3,40,41,42,43,44,45])
# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(2):
fpr[i], tpr[i], _ = roc_curve(y_test_bi, y_pred_bi)
roc_auc[i] = auc(fpr[i], tpr[i])
However, I am now getting the following error:
Traceback (most recent call last):
File "C:\Users\app\Documents\Python Scripts\gbc_classifier_test.py", line 152, in <module>
fpr[i], tpr[i], _ = roc_curve(y_test_bi, y_pred_bi)
File "C:\Users\app\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 672, in roc_curve
fps, tps, thresholds = _binary_clf_curve(y_true, y_score, pos_label)
File "C:\Users\app\Anaconda\lib\site-packages\sklearn\metrics\metrics.py", line 505, in _binary_clf_curve
y_true = column_or_1d(y_true)
File "C:\Users\app\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 265, in column_or_1d
raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (300L, 46L)
source to share
roc_curve
takes a parameter with a form [n_samples]
( link ), and your inputs ( y_test_bi
either y_pred_bi
) are forms (300, 46)
. Pay attention to the first
I think the problem y_pred_bi
is the array of probabilities created by the call clf.predict_proba(X)
(please confirm this). Since your classifier has been trained in all 46 grades, it outputs 46-dimensional vectors for each data point and cannot do anything label_binarize
.
I know two ways to get around this:
- Place 46 binary classifiers by calling
label_binarize
beforeclf.fit()
and then compute the ROC curve - Dump each column of the output array 300 by 46 and pass that as the second parameter
roc_curve
. This is my preferred approach, I assume ity_pred_bi
contains probabilities
source to share