I am trying to plot ROC curve for multi class classification.I would like to plot multiple lines in a single graph for each class.But I am getting error
Error Traceback (most recent call last): File "/home/ex/Downloads/HW/DATAfile.py", line 385, in <module> plot_roc(actuals, class_probabilities,n_class) File "/home/ex/Downloads/HW/DATAfile.py", line 230, in plot_roc fpr[i], tpr[i], _ = roc_curve(actuals[:, i], probabilities[:, i]) TypeError: list indices must be integers or slices, not tuple
I could not understand how I will get rid of this error.
Here is my code that I used
def test_class_probabilities(model, test_loader, n_class):
model.eval()
actuals = []
probabilities = []
with torch.no_grad():
for sample in test_loader:
labels = Variable(sample['grade'])
inputs = Variable(sample['image'])
outputs = net(inputs).squeeze()
prediction = outputs.argmax(dim=1, keepdim=True)
actuals.extend(labels.view_as(prediction) == n_class)
probabilities.extend(np.exp(outputs[:, n_class]))
return actuals,probabilities #[i.item() for i in actuals], [i.item() for i in probabilities]
def plot_roc( actuals, probabilities, n_classes):
"""
compute ROC curve and ROC area for each class in each fold
"""
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(actuals[:, i], probabilities[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# plt.figure(figsize=(6,6))
for i in range(n_classes):
plt.plot(fpr[i], tpr[i], label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i])) # roc_auc_score
plt.plot([0, 1], [0, 1], 'k--')
# plt.grid()
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
# plt.tight_layout()
plt.show()