Hello I need to plot the ROC Curve for this code but I cannot, I tried do many methods But never Did So please do solve my problem

username: Mohan_Rb
cooked: <p><a href="https://drive.google.com/file/d/1yxwD13XgdMvpwbva66J-CSm6YrVO2Q0i/view?usp=sharing" class="onebox" target="_blank" rel="noopener nofollow ugc">https://drive.google.com/file/d/1yxwD13XgdMvpwbva66J-CSm6YrVO2Q0i/view?usp=sharing</a></p>

username: ptrblck
cooked: <p>Could you explain the issue a bit more, which approaches you’ve tried, and where you are stuck?<br>
For a general example on plotting the ROC, please refer to e.g. <a href="https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html">this example</a>.</p>

username: Mohan_Rb
cooked: <p>hi here my code.<br>
could yo please draw the roc curve for the code in pytorch.</p>
<p>def train(epochs):</p>
<pre><code>best_model_wts = copy.deepcopy(model.state_dict())

b_acc = 0.0

t_loss = []

t_acc = []

avg_t_loss=[]

avg_t_acc=[]

v_loss = []

v_acc=[]

avg_v_loss = []

avg_v_acc = []

ep = []

print('Starting training..')

for e in range(0, epochs):

    ep.append(e+1)

    print('='*20)

    print(f'Starting epoch {e + 1}/{epochs}')

    print('='*20)

    train_loss = 0.

    val_loss = 0.

    train_accuracy = 0

    total_train = 0

    correct_train = 0

    model.train() # set model to training phase

    for train_step, (images, labels) in enumerate(dl_train):

        optimizer.zero_grad()

        outputs = model(images)

        _, pred = torch.max(outputs, 1)

        loss = loss_fn(outputs, labels)

        loss.backward()

        optimizer.step()

        train_loss += loss.item()

        train_loss /= (train_step + 1)

        _, predicted = torch.max(outputs, 1)

        total_train += labels.nelement()

        correct_train += sum((predicted == labels).numpy())

        train_accuracy = correct_train / total_train

        t_loss.append(train_loss)

        t_acc.append(train_accuracy)

        

        if train_step % 20 == 0:

            print('Evaluating at step', train_step)

            print(f'Training Loss: {train_loss:.4f}, Training Accuracy: {train_accuracy:.4f}')

            accuracy = 0.  

            model.eval() # set model to eval phase

            for val_step, (images, labels) in enumerate(dl_test):

                outputs = model(images)

                loss = loss_fn(outputs, labels)

                val_loss += loss.item()

                _, preds = torch.max(outputs, 1)

                accuracy += sum((preds == labels).numpy())

            val_loss /= (val_step + 1)

            accuracy = accuracy/len(test_dataset)

            print(f'Validation Loss: {val_loss:.4f}, Validation Accuracy: {accuracy:.4f}')

            v_loss.append(val_loss)

            v_acc.append(accuracy)

            show_preds()

            model.train()

            if accuracy &gt; b_acc:

              b_acc = accuracy

                                

    avg_t_loss.append(sum(t_loss)/len(t_loss))

    avg_v_loss.append(sum(v_loss)/len(v_loss))

    avg_t_acc.append(sum(t_acc)/len(t_acc))

    avg_v_acc.append(sum(v_acc)/len(v_acc))

    

best_model_wts = copy.deepcopy(model.state_dict())

print('Best validation Accuracy: {:4f}'.format(b_acc))            

print('Training complete..')



plt.plot(ep, avg_t_loss, 'g', label='Training loss')

plt.plot(ep, avg_v_loss, 'b', label='validation loss')

plt.title('Training and Validation loss for each epoch')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.legend()

#plt.savefig('/gdrive/My Drive/Research_Documents_completed/MobileNet/mobilenet_loss.png')

plt.show()

plt.plot(ep, avg_t_acc, 'g', label='Training accuracy')

plt.plot(ep, avg_v_acc, 'b', label='validation accuracy')

plt.title('Training and Validation Accuracy for each epoch')

plt.xlabel('Epochs')

plt.ylabel('Accuracy')

plt.legend()

#plt.savefig('/gdrive/My Drive/Research_Documents_completed/MobileNet/mobilenet_accuarcy.png')

plt.show()

#torch.save(model.state_dict(),'/gdrive/My Drive/Research_Documents_completed/MobileNet/mobilenet.pt')
</code></pre>
<p><span class="hashtag">#confsuion</span> matrix<br>
nb_classes = 2</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<p>confusion_matrix = torch.zeros(nb_classes, nb_classes)</p>
<p>with torch.no_grad():</p>
<pre><code>for i, (inputs, classes) in enumerate(dl_test):

    inputs = inputs.to(device)

    classes = classes.to(device)

    outputs = model(inputs)

    _, preds = torch.max(outputs, 1)

    for t, p in zip(classes.view(-1), preds.view(-1)):

            confusion_matrix[t.long(), p.long()] += 1
</code></pre>
<p>print(confusion_matrix)</p>
<p>tensor([[1407.,   10.],<br>
[  17., 1327.]])</p>

username: ptrblck
cooked: <p>I’m unsure where you are stuck, but the link gives the code you could probably directly use:</p>
<pre><code class="lang-python"># Compute ROC curve and ROC area for each class
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])

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

plt.figure()
lw = 2
plt.plot(fpr[2], tpr[2], color='darkorange',
         lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
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 example')
plt.legend(loc="lower right")
plt.show()
</code></pre>
<p>This will plot the ROC for a specific class and you could of course create multiple figures (or multiple curves in the same plot).</p>
<p>Based on your code you would probably have to replace <code>y_score</code> with <code>outpus</code> and <code>y_test</code> with <code>classes</code>, but since your code is not executable I cannot verify it.</p>

username: Mohan_Rb
cooked: <p>hi ptrblck</p>
<p>Actually I am doing a Project of my last year about covid detection from chest x-rays am struggling to plot sensitivity, specificity, and Roc curves if you have any code related to this then please give me the code.</p>
<p>check the link here my code.</p>
<p><a href="https://drive.google.com/file/d/1k8DW4mmqmk-w1Q9jUSGlXLpN73dgvjUh/view?usp=sharing" class="onebox" target="_blank" rel="noopener nofollow ugc">https://drive.google.com/file/d/1k8DW4mmqmk-w1Q9jUSGlXLpN73dgvjUh/view?usp=sharing</a></p>

username: Mohan_Rb
cooked: <p>Error I countered<br>
// for i in range(classes):</p>
<p>TypeError                                 Traceback (most recent call last)<br>
 in ()<br>
14 tpr = dict()<br>
15 roc_auc = dict()<br>
—&gt; 16 for i in range(classes):<br>
17     fpr[i], tpr[i], _ = roc_curve(classes[:, i], outputs[:, i])<br>
18     roc_auc[i] = auc(fpr[i], tpr[i])</p>
<p>TypeError: ‘tuple’ object cannot be interpreted as an integer</p>

username: ptrblck
cooked: <p>Could you check the types as well as shapes of all objects and post an executable code snippet which would reproduce the issue, please?</p>

username: Mohan_Rb
cooked: <h1>
<a name="import-the-required-modules-1" class="anchor" href="#import-the-required-modules-1"></a>Import the required modules</h1>
<p>import datetime</p>
<p>import pandas as pd</p>
<p>import numpy as np</p>
<p>import matplotlib</p>
<p>import matplotlib.pyplot as plt</p>
<p>%matplotlib inline</p>
<p>matplotlib.rcParams[‘font.size’] = 8</p>
<p>from skimage import img_as_float</p>
<p>from skimage import exposure</p>
<p>import plotly.graph_objects as go</p>
<p>import os</p>
<p>import glob</p>
<p>import random</p>
<p>from skimage import io # To preprocess the images</p>
<p>from distutils.file_util import copy_file</p>
<p>import seaborn as sns</p>
<p>import cv2</p>
<p>import keras</p>
<p>from keras.models import load_model</p>
<p>from keras import backend as K</p>
<p>import tensorflow as tf</p>
<p>from skimage.transform import rescale</p>
<p>from keras_preprocessing.image import ImageDataGenerator</p>
<p>import warnings</p>
<p>warnings.simplefilter(‘ignore’)</p>
<p>%matplotlib inline</p>
<p>import glob</p>
<p>import os</p>
<p>import shutil</p>
<p>import copy</p>
<p>import random</p>
<p>import torch</p>
<p>import torch.nn as nn</p>
<p>import torchvision</p>
<p>import torch.optim as optim</p>
<p>from torch.optim import lr_scheduler</p>
<p>import numpy as np</p>
<p>import seaborn as sns</p>
<p>import time</p>
<p>from sklearn.metrics import confusion_matrix</p>
<p>from PIL import Image</p>
<p>import matplotlib.pyplot as plt</p>
<p>torch.manual_seed(0)</p>
<p>print(‘Using PyTorch version’, torch.<strong>version</strong>)</p>
<p>if torch.cuda.is_available():</p>
<p>device = torch.device(“cuda:0”)</p>
<p>print(“GPU”)</p>
<p>else:</p>
<p>device = torch.device(“cpu”)</p>
<p>print(“CPU”)</p>
<p>! pip install -q kaggle</p>
<p>from google.colab import files</p>
<p>files.upload()</p>
<p>! mkdir ~/.kaggle</p>
<p>! cp kaggle.json ~/.kaggle/</p>
<p>! chmod 600 ~/.kaggle/kaggle.json</p>
<p>!  kaggle datasets download -d mohanrb/mydataset</p>
<p>! mkdir ~/.dataset</p>
<p>!unzip “/content/mydataset.zip” -d “/content/dataset”</p>
<p>class_names = [‘Non-Covid’, ‘Covid’]<br>
root_dir = ‘/content/dataset/MYDATASET’<br>
source_dirs = ['Normal ', ‘COVID’]</p>
<p>DATASET_PATH = ‘/content/dataset/MYDATASET’</p>
<h1>
<a name="there-are-two-classes-of-images-that-we-will-deal-with-2" class="anchor" href="#there-are-two-classes-of-images-that-we-will-deal-with-2"></a>There are two classes of images that we will deal with</h1>
<p>cls = [‘Normal’, ‘COVID’]</p>
<p>class ChestXRayDataset(torch.utils.data.Dataset):<br>
def <strong>init</strong>(self, image_dirs, transform):<br>
def get_images(class_name):<br>
images = [x for x in os.listdir(image_dirs[class_name]) if x.lower().endswith(‘png’) or x.lower().endswith(‘jpg’)]<br>
print(f’Found {len(images)} {class_name} examples’)<br>
return images<br>
self.images = {}<br>
self.class_names = [‘Non-Covid’, ‘Covid’]<br>
for class_name in self.class_names:<br>
self.images[class_name] = get_images(class_name)<br>
self.image_dirs = image_dirs<br>
self.transform = transform<br>
def <strong>len</strong>(self):<br>
return sum([len(self.images[class_name]) for class_name in self.class_names])</p>
<pre><code>def __getitem__(self, index):
</code></pre>
<p>class_name = random.choice(self.class_names)<br>
index = index % len(self.images[class_name])<br>
image_name = self.images[class_name][index]<br>
image_path = os.path.join(self.image_dirs[class_name], image_name)<br>
image = Image.open(image_path).convert(‘RGB’)<br>
return self.transform(image), self.class_names.index(class_name)</p>
<p>train_transform = torchvision.transforms.Compose([<br>
torchvision.transforms.Resize(size=(224, 224)),<br>
torchvision.transforms.RandomHorizontalFlip(),<br>
torchvision.transforms.ToTensor(),<br>
torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])<br>
])</p>
<p>test_transform = torchvision.transforms.Compose([<br>
torchvision.transforms.Resize(size=(224, 224)),<br>
torchvision.transforms.ToTensor(),<br>
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
])</p>
<h1>
<a name="create-new-folders-for-image-training-3" class="anchor" href="#create-new-folders-for-image-training-3"></a>Create new folders for image training</h1>
<h1>
<a name="main-folder-4" class="anchor" href="#main-folder-4"></a>main folder</h1>
<p>!mkdir ./Data/</p>
<h1>
<a name="train-data-folders-5" class="anchor" href="#train-data-folders-5"></a>Train data folders</h1>
<p>!mkdir ./Data/Train/</p>
<p>!mkdir ./Data/Train/Covid/</p>
<p>!mkdir ./Data/Train/Normal/</p>
<h1>
<a name="test-data-folders-6" class="anchor" href="#test-data-folders-6"></a>Test data folders</h1>
<p>!mkdir ./Data/Test/</p>
<p>!mkdir ./Data/Test/Covid/</p>
<p>!mkdir ./Data/Test/Normal/</p>
<h1>
<a name="paths-to-covid-images-folders-7" class="anchor" href="#paths-to-covid-images-folders-7"></a>Paths to covid images folders</h1>
<p>pathCovidTrain = ‘./Data/Train/Covid/’</p>
<p>pathCovidTest = ‘./Data/Test/Covid/’</p>
<h1>
<a name="paths-to-normal-images-folders-8" class="anchor" href="#paths-to-normal-images-folders-8"></a>Paths to normal images folders</h1>
<p>pathNormalTrain = ‘./Data/Train/Normal/’<br>
pathNormalTest = ‘./Data/Test/Normal/’</p>
<h1>
<a name="create-the-list-of-paths-to-the-images-9" class="anchor" href="#create-the-list-of-paths-to-the-images-9"></a>Create the list of paths to the images</h1>
<h1>
<a name="lists-for-access-paths-10" class="anchor" href="#lists-for-access-paths-10"></a>Lists for access paths</h1>
<p>listCovidPaths = []</p>
<p>listNormalPaths = []</p>
<h1>
<a name="get-covid-images-files-paths-11" class="anchor" href="#get-covid-images-files-paths-11"></a>Get covid images files paths</h1>
<p>for root, directories, files in os.walk(covid_path[:-2]):</p>
<pre><code>for name in files:

    listCovidPaths.append(os.path.join(root, name))
</code></pre>
<h1>
<a name="get-normal-images-files-paths-12" class="anchor" href="#get-normal-images-files-paths-12"></a>Get normal images files paths</h1>
<p>for root, directories, files in os.walk(normal_path[:-2]):</p>
<pre><code>for name in files:

    listNormalPaths.append(os.path.join(root, name))

    # Shuffle lists for random train / test
</code></pre>
<p>random.shuffle(listCovidPaths)</p>
<p>random.shuffle(listNormalPaths)</p>
<p>len_covid = len(os.listdir(covid_path[:-2]))</p>
<p>len_normal = len(os.listdir(normal_path[:-2]))</p>
<p>covid_80 = round(len(os.listdir(covid_path[:-2])) * 0.8)     # 80% of the COVID data</p>
<p>normal_80 = round(len(os.listdir(normal_path[:-2])) * 0.8)   # 80% of the Normal data</p>
<h1>
<a name="move-normal-images-files-to-new-folders-13" class="anchor" href="#move-normal-images-files-to-new-folders-13"></a>Move normal images files to new folders</h1>
<p>for i in range(len_normal):</p>
<pre><code>if i &lt; normal_80:

    copy_file(listNormalPaths[i], pathNormalTrain)

else :

     copy_file(listNormalPaths[i], pathNormalTest)
</code></pre>
<h1>
<a name="move-covid-images-files-to-new-folders-14" class="anchor" href="#move-covid-images-files-to-new-folders-14"></a>Move covid images files to new folders</h1>
<p>for i in range(len_covid):</p>
<pre><code>if i &lt; covid_80:

    copy_file(listCovidPaths[i], pathCovidTrain)

else : 

    copy_file(listCovidPaths[i], pathCovidTest)
</code></pre>
<p>train_dirs = {</p>
<pre><code>'Non-Covid': '/content/Data/Train/Normal',

'Covid': '/content/Data/Train/Covid'
</code></pre>
<p>}</p>
<p>train_dataset = ChestXRayDataset(train_dirs, train_transform)</p>
<p>test_dirs = {</p>
<pre><code>'Non-Covid': '/content/Data/Test/Normal',

'Covid': '/content/Data/Test/Covid'
</code></pre>
<p>}</p>
<p>test_dataset = ChestXRayDataset(test_dirs, test_transform)</p>
<p>batch_size = 25</p>
<p>dl_train = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)</p>
<p>dl_test = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True)</p>
<p>print(dl_train)</p>
<p>print(‘Number of training batches’, len(dl_train))</p>
<p>print(‘Number of test batches’, len(dl_test))</p>
<p>class_names = train_dataset.class_names</p>
<p>def show_images(images, labels, preds):</p>
<pre><code>plt.figure(figsize=(30, 20))

for i, image in enumerate(images):

    plt.subplot(1, 25, i + 1, xticks=[], yticks=[])

    image = image.numpy().transpose((1, 2, 0))

    mean = np.array([0.485, 0.456, 0.406])

    std = np.array([0.229, 0.224, 0.225])

    image = image * std + mean

    image = np.clip(image, 0., 1.)

    plt.imshow(image)

    col = 'green'

    if preds[i] != labels[i]:

        col = 'red'

        

    plt.xlabel(f'{class_names[int(labels[i].numpy())]}')

    plt.ylabel(f'{class_names[int(preds[i].numpy())]}', color=col)

plt.tight_layout()

plt.show()
</code></pre>
<p>model = torchvision.models.mobilenet_v2(pretrained=True)</p>
<p>print(model)</p>
<p>model.classifier[1] = torch.nn.Linear(in_features=1280, out_features=2, bias=True)</p>
<p>loss_fn = torch.nn.CrossEntropyLoss()</p>
<p>optimizer = torch.optim.Adam(model.parameters(), lr=3e-5)</p>
<p>def show_preds():</p>
<pre><code>model.eval()

images, labels = next(iter(dl_test))

outputs = model(images)

_, preds = torch.max(outputs, 1)

show_images(images, labels, preds)
</code></pre>
<p>def train(epochs):</p>
<pre><code>best_model_wts = copy.deepcopy(model.state_dict())

b_acc = 0.0

t_loss = []

t_acc = []

avg_t_loss=[]

avg_t_acc=[]

v_loss = []

v_acc=[]

avg_v_loss = []

avg_v_acc = []

ep = []

print('Starting training..')

for e in range(0, epochs):

    ep.append(e+1)

    print('='*20)

    print(f'Starting epoch {e + 1}/{epochs}')

    print('='*20)

    train_loss = 0.

    val_loss = 0.

    train_accuracy = 0

    total_train = 0

    correct_train = 0

    model.train() # set model to training phase

    for train_step, (images, labels) in enumerate(dl_train):

        optimizer.zero_grad()

        outputs = model(images)

        _, pred = torch.max(outputs, 1)

        loss = loss_fn(outputs, labels)

        loss.backward()

        optimizer.step()

        train_loss += loss.item()

        train_loss /= (train_step + 1)

        _, predicted = torch.max(outputs, 1)

        total_train += labels.nelement()

        correct_train += sum((predicted == labels).numpy())

        train_accuracy = correct_train / total_train

        t_loss.append(train_loss)

        t_acc.append(train_accuracy)

        

        if train_step % 20 == 0:

            print('Evaluating at step', train_step)

            print(f'Training Loss: {train_loss:.4f}, Training Accuracy: {train_accuracy:.4f}')

            accuracy = 0.  

            model.eval() # set model to eval phase

            for val_step, (images, labels) in enumerate(dl_test):

                outputs = model(images)

                loss = loss_fn(outputs, labels)

                val_loss += loss.item()

                _, preds = torch.max(outputs, 1)

                accuracy += sum((preds == labels).numpy())

            val_loss /= (val_step + 1)

            accuracy = accuracy/len(test_dataset)

            print(f'Validation Loss: {val_loss:.4f}, Validation Accuracy: {accuracy:.4f}')

            v_loss.append(val_loss)

            v_acc.append(accuracy)

            show_preds()

            model.train()

            if accuracy &gt; b_acc:

              b_acc = accuracy

                                

    avg_t_loss.append(sum(t_loss)/len(t_loss))

    avg_v_loss.append(sum(v_loss)/len(v_loss))

    avg_t_acc.append(sum(t_acc)/len(t_acc))

    avg_v_acc.append(sum(v_acc)/len(v_acc))

    

best_model_wts = copy.deepcopy(model.state_dict())

print('Best validation Accuracy: {:4f}'.format(b_acc))            

print('Training complete..')



plt.plot(ep, avg_t_loss, 'g', label='Training loss')

plt.plot(ep, avg_v_loss, 'b', label='validation loss')

plt.title('Training and Validation loss for each epoch')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.legend()

#plt.savefig('/gdrive/My Drive/Research_Documents_completed/MobileNet/mobilenet_loss.png')

plt.show()

plt.plot(ep, avg_t_acc, 'g', label='Training accuracy')

plt.plot(ep, avg_v_acc, 'b', label='validation accuracy')

plt.title('Training and Validation Accuracy for each epoch')

plt.xlabel('Epochs')

plt.ylabel('Accuracy')

plt.legend()

#plt.savefig('/gdrive/My Drive/Research_Documents_completed/MobileNet/mobilenet_accuarcy.png')

plt.show()

#torch.save(model.state_dict(),'/gdrive/My Drive/Research_Documents_completed/MobileNet/mobilenet.pt')
</code></pre>
<p>%%time<br>
train(epochs=3)</p>
<p>nb_classes = 2</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<p>confusion_matrix = torch.zeros(nb_classes, nb_classes)</p>
<p>with torch.no_grad():</p>
<pre><code>for i, (inputs, classes) in enumerate(dl_test):

    inputs = inputs.to(device)

    classes = classes.to(device)

    outputs = model(inputs)

    _, preds = torch.max(outputs, 1)

    for t, p in zip(classes.view(-1), preds.view(-1)):

            confusion_matrix[t.long(), p.long()] += 1
</code></pre>
<p>print(confusion_matrix)</p>
<p>import itertools</p>
<p>import numpy as np</p>
<p>import matplotlib.pyplot as plt</p>
<p>def plot_confusion_matrix(cm, classes, normalize=False, title=‘Confusion matrix’, cmap=plt.cm.Blues):</p>
<pre><code>if normalize:

    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    print("Normalized confusion matrix")

else:

    print('Confusion matrix, without normalization')

print(cm)

plt.imshow(cm, interpolation='nearest', cmap=cmap)

plt.title(title)

plt.colorbar()

tick_marks = np.arange(len(classes))

plt.xticks(tick_marks, classes, rotation=45)

plt.yticks(tick_marks, classes)

fmt = '.2f' 

thresh = cm.max() / 2.

for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):

    plt.text(float(j),float(i), format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] &gt; thresh else "black")

plt.tight_layout()

plt.ylabel('True label')

plt.xlabel('Predicted label')
</code></pre>
<p>plot_confusion_matrix(confusion_matrix,class_names)</p>
<p>import tensorflow as tf</p>
<p>FP = (confusion_matrix.sum(axis=0) - np.diag(confusion_matrix)).numpy()</p>
<p>FN = (confusion_matrix.sum(axis=1) - np.diag(confusion_matrix)).numpy()</p>
<p>TP = np.diag(confusion_matrix)</p>
<p>TN = (confusion_matrix.sum() - (FP + FN + TP)).numpy()</p>
<p>FP = FP.astype(float)</p>
<p>FN = FN.astype(float)</p>
<p>TP = TP.astype(float)</p>
<p>TN = TN.astype(float)</p>
<h1>
<a name="sensitivity-hit-rate-recall-or-true-positive-rate-15" class="anchor" href="#sensitivity-hit-rate-recall-or-true-positive-rate-15"></a>Sensitivity, hit rate, recall, or true positive rate</h1>
<p>TPR = TP/(TP+FN)</p>
<h1>
<a name="specificity-or-true-negative-rate-16" class="anchor" href="#specificity-or-true-negative-rate-16"></a>Specificity or true negative rate</h1>
<p>TNR = TN/(TN+FP)</p>
<h1>
<a name="precision-or-positive-predictive-value-17" class="anchor" href="#precision-or-positive-predictive-value-17"></a>Precision or positive predictive value</h1>
<p>PPV = TP/(TP+FP)</p>
<h1>
<a name="negative-predictive-value-18" class="anchor" href="#negative-predictive-value-18"></a>Negative predictive value</h1>
<p>NPV = TN/(TN+FN)</p>
<h1>
<a name="fall-out-or-false-positive-rate-19" class="anchor" href="#fall-out-or-false-positive-rate-19"></a>Fall out or false positive rate</h1>
<p>FPR = FP/(FP+TN)</p>
<h1>
<a name="false-negative-rate-20" class="anchor" href="#false-negative-rate-20"></a>False negative rate</h1>
<p>FNR = FN/(TP+FN)</p>
<h1>
<a name="false-discovery-rate-21" class="anchor" href="#false-discovery-rate-21"></a>False discovery rate</h1>
<p>FDR = FP/(TP+FP)</p>
<h1>
<a name="overall-accuracy-for-each-class-22" class="anchor" href="#overall-accuracy-for-each-class-22"></a>Overall accuracy for each class</h1>
<p>ACC = (TP+TN)/(TP+FP+FN+TN)</p>
<p><span class="hashtag">#its</span> my code<br>
<span class="hashtag">#so</span> far I completed and I got confusion matrix but further plotting sensitivity, specificity, and ROC curves  iam  troubling please give the solution.</p>

username: Mohan_Rb
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/a/1a2489070618b7430962dddd7b354812f9bec36c.png" data-download-href="https://discuss.pytorch.org/uploads/default/1a2489070618b7430962dddd7b354812f9bec36c" title="roc"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1a2489070618b7430962dddd7b354812f9bec36c_2_690x152.png" alt="roc" data-base62-sha1="3JgGOWBKRwQ8xKZC0EuyZupNAKM" width="690" height="152" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1a2489070618b7430962dddd7b354812f9bec36c_2_690x152.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1a2489070618b7430962dddd7b354812f9bec36c_2_1035x228.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1a2489070618b7430962dddd7b354812f9bec36c_2_1380x304.png 2x" data-dominant-color="F5F1F0"><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">roc</span><span class="informations">1699×375 24.4 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><span class="hashtag-raw">#Error</span> in ROC code</p>

username: ptrblck
cooked: <p>Your code is unfortunately not executable and also doesn’t show a minimal snippet yielding the error. You can also format code by wrapping it into three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=10" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Based on the screenshot, <code>range(classes)</code> raises the error, since <code>classes</code> is a <code>tuple</code> while an <code>int</code> is expected.</p>

username: Mohan_Rb
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/3/d34c852e2c4890641da705f93459f81a065d1fff.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d34c852e2c4890641da705f93459f81a065d1fff" title="err1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d34c852e2c4890641da705f93459f81a065d1fff_2_690x220.jpeg" alt="err1" data-base62-sha1="u9eGMNrWiCSxexg4uAqkt35itNd" width="690" height="220" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d34c852e2c4890641da705f93459f81a065d1fff_2_690x220.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d34c852e2c4890641da705f93459f81a065d1fff_2_1035x330.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d34c852e2c4890641da705f93459f81a065d1fff_2_1380x440.jpeg 2x" data-dominant-color="F3F0EF"><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">err1</span><span class="informations">1686×538 82.7 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/b/0/b0f82c3a016df0c3c80cd0d28a6c769d860cc179.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b0f82c3a016df0c3c80cd0d28a6c769d860cc179" title="err2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0f82c3a016df0c3c80cd0d28a6c769d860cc179_2_690x277.jpeg" alt="err2" data-base62-sha1="pfxK272FyQM0UOX8twxeXdpIETT" width="690" height="277" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0f82c3a016df0c3c80cd0d28a6c769d860cc179_2_690x277.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/0/b0f82c3a016df0c3c80cd0d28a6c769d860cc179_2_1035x415.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/0/b0f82c3a016df0c3c80cd0d28a6c769d860cc179.jpeg 2x" data-dominant-color="F4F4F6"><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">err2</span><span class="informations">1224×493 78.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></p>
<p>THE following error occurred.</p>