1

I need to calculate TP, FP, TN and FN values from my confusion matrix for a multi class classification problem.

As I need to get sensitivity and specificity.

Here is what my confusion matrix looks like, I have 4 classes total:

[[302 23 102 15]
[34 56 34 340]
[34 32 69 54]
[231 89 32 34]]

Here are parts of my code

#loading data using generator with class mode = categorical
test_datagen = ImageDataGenerator(rescale = 1./255)
test_set = test_datagen.flow_from_directory('animals/valid/',
target_size=(150,150),class_mode='categorical',batch_size=32)

#compile the model with categorical cross entropy
model.compile(loss='categorical_crossentropy',optimizer=Adam(lr=0.00001),metrics=['accuracy'])

#calculate confusion matrix
test_im, test_lbl = next(test_set)
predections = model.predict(test_im)
predections = np.argmax(predections, axis = 1)
test_lbl = np.argmax(test_lbl, axis = 1)
conf_mat = confusion_matrix(all_labels, all_predications)

Also is it correct that I am using this approach in calculating conduction matrix given that classes are loaded in one hot encoding values because of using class_mode='categorical'in the image generator.

s.ali
  • 146
  • 1
  • 8
  • Please check out: https://stackoverflow.com/questions/50666091/true-positive-rate-and-false-positive-rate-tpr-fpr-for-multi-class-data-in-py – Ashwin Geet D'Sa Feb 14 '19 at 07:01

1 Answers1

0

TP-Diagonal elements (302,56,69,34)

FP -[23 102 15],[34 34 340],[34 32 54],[231 89 32]

TN - No vals

FN - No vals

Safvan CK
  • 1,140
  • 9
  • 18
  • Do you mean there will be no values for TN/FN in multi class classification problems? Accuracy for example is (TP+TN)/total. So will that be (TP+0)/total in the case of multi class classification. – s.ali Feb 15 '19 at 07:40