2

I'm new to SciKit and Python.
Currently I am trying to generate a multiclass (3 classes) ROC curve from csv file that looks like this:

  probability,predclass,dist0,dist1,dist2,actualclass
  99.94571208953857,1,0.00022618949060415616,99.94571208953857,0.054055178770795465,1
  99.99398589134216,0,99.99398589134216,0.001082851395040052,0.004925658140564337,0
  99.97997879981995,1,0.015142260235734284,99.97997879981995,0.004879535117652267,1
  93.58544945716858,2,5.507804825901985,0.9067309089004993,93.58544945716858,2
  92.31788516044617,1,7.572370767593384,92.31788516044617,0.10974484030157328,1
  62.839555740356445,1,2.3740695789456367,62.839555740356445,34.786370396614075,2
        ... 

and my current code is:

df = pd.read_csv('mydata.csv')
pred = (  df.loc[:,['dist0','dist1','dist2']])/100
actual = df['actualclass']
fpr, tpr, _ = roc_curve(actual, pred)

I have tried solution from this question: https://stackoverflow.com/a/45335434/14482749

by doing:

for i in range(n_classes):
    fpr[i], tpr[i], _ = roc_curve(actual, pred[:, i]) 
    roc_auc[i] = auc(fpr[i], tpr[i])

but received an error : TypeError: '(slice(None, None, None), 0)' is an invalid key at line 2 above.

I believe the problem is my var 'actual' but I am not sure what is it

Curerious
  • 31
  • 5

1 Answers1

0

Alrighty I figured it out (my way)

so we need to feed dist0..2 and actualclassinto roc_curve function like:

for i in range(n_classes):
    fpr[i], tpr[i], _ = roc_curve(actual[:,i], pred[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])

so we can iterate for each n classes

for actual i did:

actual2Dict = { 0: [1,0,0] , 1 : [0,1,0]  , 2 : [0,0,1] } 
actual2 = df['actualclass'].map(actual2Dict) 
 
actualnew = []
for i in actual2:
    i = np.array(i)
    actualnew.append(i)
    
actualnew = np.array(actualnew)

as for pred i did:

pred = pred.to_numpy()

and from there i begin ploting:

for i in range(n_classes):
    fpr[i], tpr[i], _ = roc_curve(actualnew[:,i], pred[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])

lw = 2

# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(actualnew.ravel(), pred.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))

# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
    mean_tpr += interp(all_fpr, fpr[i], tpr[i])

# Finally average it and compute AUC
mean_tpr /= n_classes

fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
    
# Plot all ROC curves
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
         label='micro-average ROC curve (area = {0:0.2f})'
               ''.format(roc_auc["micro"]),
         color='deeppink', linestyle=':', linewidth=4)

plt.plot(fpr["macro"], tpr["macro"],
         label='macro-average ROC curve (area = {0:0.2f})'
               ''.format(roc_auc["macro"]),
         color='navy', linestyle=':', linewidth=4)

colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
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.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.show()

the code excerpt for plotting the multiclass roc_curve is from: https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html

please do share if you have a different approach!

Curerious
  • 31
  • 5