Logic error for calculate ROC & AUC in multiclass classification

faris: <p>hi i have problem in calculate the AUC in multiclass classification the code is worked and give the result but the result is lower than which should be i don’t now what the problem<br>
see the result of confusion matrix in class 2 it was classified all the images in right class but in the AUC of this class its 0.82 ?? must be 1.00</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/f/9fed34ffa8fcb8e678f73316281ff670f0dd34da.png" data-download-href="https://discuss.pytorch.org/uploads/default/9fed34ffa8fcb8e678f73316281ff670f0dd34da" title="لقطة شاشة 2024-02-05 145435"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9fed34ffa8fcb8e678f73316281ff670f0dd34da_2_690x465.png" alt="لقطة شاشة 2024-02-05 145435" data-base62-sha1="mOM89wJ0Q1HCwd0Su5hKtf3SSxA" width="690" height="465" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9fed34ffa8fcb8e678f73316281ff670f0dd34da_2_690x465.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9fed34ffa8fcb8e678f73316281ff670f0dd34da_2_1035x697.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/f/9fed34ffa8fcb8e678f73316281ff670f0dd34da.png 2x" data-dominant-color="757569"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">لقطة شاشة 2024-02-05 145435</span><span class="informations">1145×773 36.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/5/95ba7dfd2732a530ecf7fb9c3aff4f655e6e85e8.png" data-download-href="https://discuss.pytorch.org/uploads/default/95ba7dfd2732a530ecf7fb9c3aff4f655e6e85e8" title="لقطة شاشة 2024-02-05 145642"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/5/95ba7dfd2732a530ecf7fb9c3aff4f655e6e85e8_2_648x500.png" alt="لقطة شاشة 2024-02-05 145642" data-base62-sha1="lmyHrnWYLcoU5ksAbzRzyP8SDfW" width="648" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/5/95ba7dfd2732a530ecf7fb9c3aff4f655e6e85e8_2_648x500.png, https://discuss.pytorch.org/uploads/default/original/3X/9/5/95ba7dfd2732a530ecf7fb9c3aff4f655e6e85e8.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/5/95ba7dfd2732a530ecf7fb9c3aff4f655e6e85e8.png 2x" data-dominant-color="D7D9DA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">لقطة شاشة 2024-02-05 145642</span><span class="informations">730×563 79.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>the code of AUC</p>
<pre><code class="lang-auto">
def auc_and_roc_curve (lab_real, lab_pred, class_names, class_to_compute='all', save_path=None):
    """
  This function computes the ROC curves and AUC for each class.
   It better described on: https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html#sphx-glr-auto-examples-model-selection-plot-roc-py

    Both lab_real and lab_pred can be a labels array or and a array of scores (one hot encoding) for each class.

    :param lab_real (np.array): the data real labels
    :param lab_pred (np.array): the predictions returned by the model
    :param class_names (list): the name of each label. For example: ['l1','l2']. If you pass a list with a different
    :param class_to_compute (string, optional): select the class you'd like to compute the ROC. If you set 'all', it
    will compute all curves. Note that you should inform a valid class, that is, a class that is inside in class_name.
    Default is 'all'.
    :return: a dictionaty with the AUC, fpr, tpr for each class
    """

    # Checkin the array dimension
    lab_real, lab_pred = _check_dim(lab_real, lab_pred, mode='scores')

    # Computing the ROC curve and AUC for each class
    fpr = dict()  # false positive rate
    tpr = dict()  # true positive rate
    roc_auc = dict()  # area under the curve
    for i, name in enumerate(class_names):
        # print(i, name)
        fpr[name], tpr[name], _ = skmet.roc_curve(lab_real[:, i], lab_pred[:, i])
        roc_auc[name] = skmet.auc(fpr[name], tpr[name])

    if class_to_compute == 'all':

        # Compute macro-average ROC curve and ROC area
        # First aggregate all false positive rates
        all_fpr = np.unique(np.concatenate([fpr[name] for name in class_names]))

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

        # Finally average it and compute AUC
        mean_tpr /= float(len(class_names))

        fpr["macro"] = all_fpr
        tpr["macro"] = mean_tpr
        roc_auc["macro"] = skmet.auc(fpr["macro"], tpr["macro"])

        # Computing the micro-average ROC curve and the AUC
        fpr["micro"], tpr["micro"], _ = skmet.roc_curve(lab_real.ravel(), lab_pred.ravel())
        roc_auc["micro"] = skmet.auc(fpr["micro"], tpr["micro"])

        if save_path:
            # Ploting all ROC curves
            plt.figure()

            # Plotting the micro avg
            plt.plot(fpr["micro"], tpr["micro"],
                     label='MicroAVG - AUC: {0:0.2f}'
                           ''.format(roc_auc["micro"]),
                     color='deeppink', linestyle=':', linewidth=2)

            # Plotting the micro avg
            plt.plot(fpr["macro"], tpr["macro"],
                     label='MacroAVG - AUC: {0:0.2f}'
                           ''.format(roc_auc["macro"]),
                     color='navy', linestyle=':', linewidth=2)

            # Plottig the curves for each class
            for name in class_names:
                plt.plot(fpr[name], tpr[name], linewidth=1,
                         label='{0} - AUC: {1:0.2f}'
                               ''.format(name, roc_auc[name]))

    else:

        if save_path:
            plt.plot(fpr[class_to_compute], tpr[class_to_compute], linewidth=1,
                     label='{0} - AUC: {1:0.2f}'
                           ''.format(class_to_compute, roc_auc[class_to_compute]))

    if save_path:
        plt.plot([0, 1], [0, 1], 'k--', linewidth=1)
        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.title('ROC curves')
        plt.legend(loc="lower right")

        if isinstance(save_path, str):
            plt.savefig(save_path)
            plt.clf()
        elif save_path:
            plt.show()


    return roc_auc, fpr, tpr
</code></pre>
<p>the called after test phase was done</p>
<pre><code class="lang-auto">from sklearn.metrics import roc_auc_score

from sklearn.preprocessing import LabelBinarizer

import matplotlib.image as mpimg

lb = LabelBinarizer()

lb.fit(val_labels)

test_labels_onehot = lb.transform(test_labels)

test_predicted_onehot = lb.transform(test_predicted)

roc_auc2, fpr, tpr = auc_and_roc_curve(test_labels_onehot, test_predicted_onehot, class_names, save_path="/content/drive/MyDrive/my_models/test_img_roc_curve.png")

print(f"Test AUC for Each Class: {roc_auc2}")

avg_auc2 = sum(roc_auc2.values()) / len(roc_auc2)

print(f"Average AUC: {avg_auc2}")

print(f"Macro-average AUC: {roc_auc2['macro']}")

img = mpimg.imread('/content/drive/MyDrive/my_models/test_img_roc_curve.png')

# Display the image

imgplot = plt.imshow(img)

plt.show()
</code></pre>
<p>Thank you …</p>
<p>if an expert need to understand the problem i can send the notebook file in colab</p>